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


  1. Naamini Yonazi Software Developer DataVision International Beyond Android's Room: Data

    Persistence @naaminiyonazi Droidcon KE August 9, 2019
  2. 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.
  3. None
  4. 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
  5. Good offline experience when the app loses connectivity.

  6. WHAT I WAS DOING BEFORE … What happens in Room

    1, stays in Room 1 :-)
  7. • boilerplate code:

  8. • migrations

  9. • Testing was chaotic.. • Database operation on the main


  11. 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.
  12. Creating a new project Important or this error will occur

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

    for you ... NULLABLE Table in your database NON_NULL No column name
  14. Crud Operations: • INSERT • UPDATE • DELETE • QUERY

    (SELECT * FROM Task) Synchronous
  15. @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
  16. Database/Abstract class... • Update your versions • Annotation: Database and

    Version • Must extend RoomDatabase
  17. 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
  18. None
  19. • Room doesn’t allow you to access the database operations

    on the main thread … • Async Task can be used
  20. remember...Synchronous? Not a good programming practice.. so...

  21. 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.
  22. 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
  23. We like to have our application more user-friendly ...

  24. 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:
  25. None
  26. None
  27. Naamini Yonazi @naaminiyonazi Location naaminicyonazi@gmail.com .naaminicyonazi.com There is always ROOM

    for improvement!