08-4. Room, LiveData, and ViewModel
08-4. Room, LiveData, and ViewModel
Storing data
developer.android.com/arc
h
Single source of
truth for all app data;
Repository clean API for UI to
communicate with
WordListAdapter
LiveData<List<Word>>
The RoomWordsSample
WordViewModel
app that you build in the
WordRepository practical implements this
Room
Word architecture
WordRoomDatabase
SQLite WordDao
● 1 instance = 1 row
RoomDatabase
● Member variable = column LiveData
LiveData
Entity
name DAO
SQLite
@ColumnInfo(name = "first_name")
private String firstName;
@ColumnInfo(name = "last_name")
private String lastName;
@Entity(tableName = "word_table")
@NonNull
first_name last_name
@ColumnInfo(name = "first_name")
private String firstName;
@ColumnInfo(name = "last_name")
private String lastName;
● be public
OR
pet
name
owner
Activity Instance
ViewModel
Activity UI
Rotation Event
Data
Re-created Activity
Instance
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 Creative
Android Developer Fundamentals V2 Room, LiveData, Commons Attribution 4.0 International 68
and ViewModel License.
No memory leaks
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void start() {...}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void start() {...}