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

Room Persistence Library with Live Data and Kotlin Coroutines

Room Persistence Library with Live Data and Kotlin Coroutines

Mohamed Guendouz

June 03, 2020
Tweet

More Decks by Mohamed Guendouz

Other Decks in Programming

Transcript

  1. Dr. Mohamed Guendouz Android GDE | Assistant Professor @guenodz Room

    Persistence Library with Live Data and Kotlin Coroutines
  2. No registration needed Weekly Challenges & Winners! Qwiklabs Code: bit.ly/menadd-apr

    Feedback form: bit.ly/menadd-feedback Sign for the challenge: bit.ly/menadd-challenge MENA Digital Days - Together we learn! Stay Home! Stay Safe! Each week Sunday-Thursday 7-11pm (Dubai Time) Content: 20+ LIVE sessions:Talk,workshops, hackathons Speakers: Googlers, 100+ Experts & 200+ communities. Website: bit.ly/menadd-website YouTube: bit.ly/menadd-live
  3. Two more things ✌ Help us improve this initiative 1.

    ATTENDANCE CERTIFICATE weekly after filling feedback bit.ly/menadd-feedback 2. JOIN MENA DIGITAL DAYS CHALLENGE … bit.ly/menadd-challenge All winners who solve 5 quests between March - June Wins SPECIAL GIFT BOX!
  4. What is Data Persistence ? • Data Persistence means saving/storing

    data for later use. • Data can be files (photos,audio/video), user settings, structured data (SQL tables). • Android provides different solutions for each case.
  5. Data Persistence in Android 1. SharedPreferences: for storing basic data

    (user settings, game score). 2. Local Files: for saving raw data files (Audio/Video, Photos, Text files). 3. SQLite Databases: for saving structured data in SQL tables (posts, events).
  6. SQLite for Android • Pros: ◦ Native support ◦ SQL

    ◦ ... • Cons: ◦ A lot of boilerplate code ◦ Hard TDD ◦ ... •
  7. Room • Room definition: “The Room persistence library provides an

    abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.” • Room is an advanced ORM (Object Relational Mapping) library for Android, part of the Architecture Components.
  8. Adding Room to your project In build.gradle (app level) def

    room_version = "2.2.5" implementation "androidx.room:room-runtime:$room_version" annotationProcessor "androidx.room:room-compiler:$room_version" //ViewModel & LiveData implementation "android.arch.lifecycle:extensions:1.1.1" annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
  9. @Entity • An entity class represents a table, it maps

    its columns into class attributes. • One entity class for each table within the database.
  10. @DAO (Data Access Object) • A DAO class contains CRUD

    operations for a particular table. • No implementation needed.
  11. @Database • A database class represents the actual Room database.

    • Includes database entities. • Provides DAO implementations. • Main access point