10.1 - Room, LiveData, and ViewModel
10.1 - Room, LiveData, and ViewModel
Storing data
with Room
Lesson 10
developer.android.com/arch
Single source of
truth for all app data;
Repository clean API for UI to
communicate with
WordListAdapter
Room
WordRepository
practical implements this
Word architecture
WordRoomDatabase
SQLite WordDao
connect to database
This work is licensed under a
Android Developer Fundamentals V2 Room, LiveData, Creative Commons Attribution 4.0 Inter 12
and ViewModel national License
Entity
● 1 instance = 1 row
RoomDatabase
● Member variable = column LiveData
LiveData
Entity
@ColumnInfo(name = "first_name")
private String firstName;
@ColumnInfo(name = "last_name")
private String lastName;
@Entity(tableName = "word_table")
@NonNull
● be public
OR
pet
name
owner
This work is licensed under a
Android Developer Fundamentals V2 Room, LiveData, Creative Commons Attribution 4.0 Inter 22
and ViewModel national License
Many more annotations
Activity Instance
ViewModel
Activity UI
Rotation Event
Data
Re-created Activity
Instance
libraries
● Implement repository to
provide single, clean API
to app data
Dao Network
When you pass live data through the layers of your app
architecture, from a Room database to your UI, that data
must be LiveData in all layers:
● DAO
● ViewModel
● Repository
mModel.getCurrentName().observe(this, nameObserver);
This work is licensed under a
Android Developer Fundamentals V2 Room, LiveData, Creative Commons Attribution 4.0 Inter 68
and ViewModel national License
No memory leaks
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void start() {...}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void start() {...}