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

Android Room persistence library

Android Room persistence library

-what is room?
-why room?
-room vs sql (code implementation)

ValentineRutto

October 05, 2018
Tweet

More Decks by ValentineRutto

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. Room Components: ENTITY • represents a table in the database

    • has get/set field values • @entity annotation
  4. 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; }
  5. 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
  6. Room Components: DAO @Dao public interface NoteDao { @Query("SELECT *

    FROM user "+ Constants.TABLE_NAME_NOTE) List<Note> getAll(); @Insert void insert(Note note); @Update void update(Note repos); @Delete void delete(Note note);
  7. 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.
  8. Room Components: DATABASE @Database(entities = { Note.class }, version =

    1) public abstract class NoteDatabase extends RoomDatabase { public static NoteDatabase getInstance(Context context) { } }
  9. 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.
  10. Resources • Android room with a view codelab • Room

    persistence library • Notes app with room • Sqlite with room