Slide 1

Slide 1 text

Naamini Yonazi Software Developer DataVision International Beyond Android's Room: Data Persistence @naaminiyonazi Droidcon KE August 9, 2019

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Good offline experience when the app loses connectivity.

Slide 6

Slide 6 text

WHAT I WAS DOING BEFORE … What happens in Room 1, stays in Room 1 :-)

Slide 7

Slide 7 text

● boilerplate code:

Slide 8

Slide 8 text

● migrations

Slide 9

Slide 9 text

● Testing was chaotic.. ● Database operation on the main thread.

Slide 10

Slide 10 text

*ROOM* SOLVED ALL EVERYTHING!

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

Creating a new project Important or this error will occur Android room persistent: AppDatabase_Impl does not exist

Slide 13

Slide 13 text

Room will take care of the creation of the table for you ... NULLABLE Table in your database NON_NULL No column name

Slide 14

Slide 14 text

Crud Operations: ● INSERT ● UPDATE ● DELETE ● QUERY (SELECT * FROM Task) Synchronous

Slide 15

Slide 15 text

@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

Slide 16

Slide 16 text

Database/Abstract class... ● Update your versions ● Annotation: Database and Version ● Must extend RoomDatabase

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

● Room doesn’t allow you to access the database operations on the main thread … ● Async Task can be used

Slide 20

Slide 20 text

remember...Synchronous? Not a good programming practice.. so...

Slide 21

Slide 21 text

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.

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

We like to have our application more user-friendly ...

Slide 24

Slide 24 text

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:

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Naamini Yonazi @naaminiyonazi Location [email protected] .naaminicyonazi.com There is always ROOM for improvement!