Slide 1

Slide 1 text

ANDROID ROOM Deep dive into Room persistence Library

Slide 2

Slide 2 text

ABOUT ME • Android Developer at Kountable • Basketball • Poet [email protected] @Valentinerutto

Slide 3

Slide 3 text

Its a global trade finance and technology company that patners with SMEs to execute their projects

Slide 4

Slide 4 text

What is Room? • This acts as an abstraction layer for the existing SQLite APIs. • Room persistence library helps you manage your sql database • Part of android jetpack Architecture components • Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite

Slide 5

Slide 5 text

Room vs Sqlite • Room eliminates the boilerplate code -that you write when converting sqlite queries to java objects • Automatic updates on affected queries when schema changes • Compile time verification of raw sqlite queries -if you have an error in your query it will give an exception during compile time unlike sqlite which gives during run time

Slide 6

Slide 6 text

Room Components • Entity • DAO - Data Access Objects • Database

Slide 7

Slide 7 text

Room Components: ENTITY • represents a table in the database • has get/set field values • @entity annotation

Slide 8

Slide 8 text

Room Components: ENTITY @Entity public class Note { @PrimaryKey(autoGenerate = true) private int note_id; @ColumnInfo(name = "note_content") private String content; private String title; public int getNote_id() { return note_id; } public void setNote_id(int note_id) { this.note_id = note_id; }

Slide 9

Slide 9 text

Room Components: DAO • Data access Objects • Defines all methods that access data • contains all methods defining operations to be done on data • Crud methods • @Dao annotation

Slide 10

Slide 10 text

Room Components: DAO @Dao public interface NoteDao { @Query("SELECT * FROM user "+ Constants.TABLE_NAME_NOTE) List getAll(); @Insert void insert(Note note); @Update void update(Note repos); @Delete void delete(Note note);

Slide 11

Slide 11 text

Room Components: DATABASE class that's annotated with @Database should satisfy the following conditions: ● Be an abstract class that extends RoomDatabase. ● Include the list of entities associated with the database within the annotation. ● Contain an abstract method that has 0 arguments and returns the class that is annotated with @Dao.

Slide 12

Slide 12 text

Room Components: DATABASE @Database(entities = { Note.class }, version = 1) public abstract class NoteDatabase extends RoomDatabase { public static NoteDatabase getInstance(Context context) { } }

Slide 13

Slide 13 text

Room Architecture

Slide 14

Slide 14 text

Room Annotations -used to import room packages,methods , variables ..etc

Slide 15

Slide 15 text

Room vs Sqlite: Sqlite Demo Making notes app with Sqlite Blog Full code on Github

Slide 16

Slide 16 text

Room vs Sqlite: Room Demo Making notes app with Room Blog Full code on Github

Slide 17

Slide 17 text

Conclusion • It is easier than using SQLite directly. • It makes you verify your database code during compile time. • It makes your code more modular and readable as you divide your database code in three components like Entity , DAO and Database.

Slide 18

Slide 18 text

Resources • Android room with a view codelab • Room persistence library • Notes app with room • Sqlite with room

Slide 19

Slide 19 text

Thank You [email protected] @Valentinerutto