Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beyond Android's Room: Data Persistence

Beyond Android's Room: Data Persistence

Learn how to make your life easier with data persistence with Android. We will go through entities, migrations, queries, how to handle multi-threading, difference between Room and other Data persistence and finally testing.

There’s always Room for improvement! ;-)

*This was presented in Nairobi, Kenya for the DroidconKE 2019 event

Naamini Yonazi

August 09, 2019
Tweet

More Decks by Naamini Yonazi

Other Decks in Programming

Transcript

  1. Room? - Android Architecture Component - Google IO, 2017 -

    Is an (another) ORM solution for the Android developers. - Library provides an abstraction layer over SQLite, robust, full power of SQLite. - Quickly create sqlite databases and perform CRUD operations Others most popular ones are probably ORMLite, GreenDAO and DbFlow.
  2. Why Room? • Since Room works on SQL, you can

    write your query easily. • You can use LiveData with Room in a few steps. • The library takes up only 50KB. • Room also provides easy migration mechanism which is fully tested
  3. Room’s Components Three main Components (annotated class): 1. Entity -

    creating database table/class 2. DAO - Data Access Object used to access data from the database; has methods..It is an interface 3. Database: It is an abstract class where we define our db. Extends RoomDatabase.
  4. Creating a new project Important or this error will occur

    Android room persistent: AppDatabase_Impl does not exist
  5. Room will take care of the creation of the table

    for you ... NULLABLE Table in your database NON_NULL No column name
  6. @Dao • To access your data using room’s persistence database,

    you need to work with @Dao • They offer abstract access to your apps database • Dao can be an interface/abstract
  7. Database/Abstract class... • Create your DB with the Db Builder

    • Specify db name Main access point underlying connection to your app’s persisted and relational data
  8. • Room doesn’t allow you to access the database operations

    on the main thread … • Async Task can be used
  9. Migrations.. java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like

    you’ve changed schema but forgot to update the version number. You can simply fix this by increasing the version number. java.lang.IllegalStateException: A migration from 1 to 2 is necessary. Please provide a Migration in the builder or call fallbackToDestructiveMigration in the builder in which case Room will re-create all of the tables.
  10. Now Room doesn’t know how to migrate database from version

    1 to version 2. In this error Room suggest us two solutions: • drop and recreate the whole database • upgrade existing database schema to the newer version
  11. android { javaCompileOptions { annotationProcessorOptions { arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]

    } } } } Thanks to this code fragment added to build.gradle file we’ll be able to get those auto-generated by Room SQL statements and use them to make a migration: