Chapter 14. Room Databases: Room with a View
Most apps need data that persists.
But if you don’t take steps to store this data somewhere, it will be lost forever as soon as the app is closed down. You usually keep data safe in Androidville by storing it in a database, so in this chapter, we’ll introduce you to the Room persistence library. You’ll learn how to build databases, create tables, and define data access methods using annotated classes and interfaces. You’ll find out how to use coroutines to run database code in the background. And along the way, you’ll discover how to transform your live data as soon as it changes with a little help from Transformations.map().
Most apps need to store data
Nearly all of the apps that you’ve written so far have used a small amount of static data in order to function. The Guessing Game app we built in Chapter 11 to Chapter 13, for example, holds an array of Strings in its view model so that the game can select one at random for you to guess.
In the real world, however, most apps require more than just static data; they need to be able to save data that can change so that it doesn’t get lost when the user shuts down the app. A music app might need to store playlists, for example, and a game might need to record the user’s progress so that they can go back to where they left off.
Apps can persist data using a database
In most cases, the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access