Features ● Wrapper around SQLite ● Eliminates boilerplate code ● Upgrade database versions with ease - MIGRATION ● Enforces you don’t perform database operations on the main thread ● Checks query at compile time ● Testing is easier
Features ● Wrapper around SQLite ● Eliminates boilerplate code ● Upgrade database versions with ease - MIGRATION ● Enforces you don’t perform database operations on the main thread ● Checks query at compile time ● Testing is easier
Features ● Wrapper around SQLite ● Eliminates boilerplate code ● Upgrade database versions with ease - MIGRATION ● Enforces you don’t perform database operations on the main thread ● Checks query at compile time ● Testing is easier
Features ● Wrapper around SQLite ● Eliminates boilerplate code ● Upgrade database versions with ease - MIGRATION ● Enforces you don’t perform database operations on the main thread ● Checks query at compile time ● Testing is easier
Features ● Wrapper around SQLite ● Eliminates boilerplate code ● Upgrade database versions with ease - MIGRATION ● Enforces you don’t perform database operations on the main thread ● Checks query at compile time ● Testing is easier
Features ● Wrapper around SQLite ● Eliminates boilerplate code ● Upgrade database versions with ease - MIGRATION ● Enforces you don’t perform database operations on the main thread ● Checks query at compile time ● Testing is easier
Major Components ● @Entity - Defines table structure ● @DAO - An interface or abstract class. The functions define how to access the database ● @Database - Connects all the pieces of Room together
Create the DAO @Dao public interface AttendeeDao { @Insert/@delete/@update void insert/delete/update (Attendee attendee); @Query("Select * from attendee where email=:email") Attendee getAttendedByEmail (String email); @Query("Select * from attendee") List getAllAttendees (); } Return a list
Create the DAO @Dao public interface AttendeeDao { @Query("Select * from attendeeess where email=:email") Attendee getAttendedByEmail (String email); } Compile Time Error
Create the database class @Database(entities = {Attendee.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { public abstract AttendeeDao attendeeDao (); }
Get the database instance public static AppDatabase database; database = Room.databaseBuilder (this, AppDatabase.class, "database_name") .build (); CAUTION!!! Recommended to keep it a singleton ‘cos database calls are not cheap
Access the DB - Insert new record Attendee attendee = new Attendee (1, "Omolara Adejuwon", "[email protected]", "Speaker"); RoomApp.database.attendeeDao ().insert (attendee);
Scenarios and actions ● Modify schema, no change in version - IllegalStateException ● Increase version, no provision for migration - IllegalStateException
Scenarios and actions ● Modify schema, no change in version - IllegalStateException ● Increase version, no provision for migration - IllegalStateException ● Increase version, enable fallback to destructive migration - wipes data
Scenarios and actions ● Modify schema, no change in version - IllegalStateException ● Increase version, no provision for migration - IllegalStateException ● Increase version, enable fallback to destructive migration - wipes data ● Increase version, provide migration - preserves data
Fallback to destructive migration? database = Room.databaseBuilder (this, AppDatabase.class, "database_name") .fallbackToDestructiveMigration () .build (); Wipe the data. I don’t care
Modify Schema, Provide Migration @Database(entities = {Attendee.class}, version = 2) public abstract class AppDatabase extends RoomDatabase { public abstract AttendeeDao attendeeDao (); } Change version number
Testing Migrations - Create test rule @Rule public MigrationTestHelper mMigrationTestHelper = new MigrationTestHelper (InstrumentationRegistry.getInstrumentation (), AppDatabase.class.getCanonicalName (), new FrameworkSQLiteOpenHelperFactory ());
Show Me The Code I have provided a repo that demonstrates all that have been said. It has two branches: room-kotlin and room-java Check it out: https://github.com/larikraun/RoomDevfestSW17