Lesson 5 - Android Architecture Components and Room Database - vVLE
Lesson 5 - Android Architecture Components and Room Database - vVLE
Database
IT6306 - Mobile Application
Development
Level III - Semester 56
2
© 2022 e-Learning Centre, UCSC
Intended Learning Outcomes
3
© 2022 e-Learning Centre, UCSC
List of sub topics
5.1. Introduction to Android Architecture Components
5.1.1. Activity / Fragment
5.1.2. ViewModel
5.1.3. Repository
5.2. Room Database
5.2.1. Room Overview
5.2.2. Components of Room
5.2.2.1. Entity
5.2.2.2. DAO (Data Access Object)
5.2.2.3. Database
5.3. Lifecycle-aware Components
5.3.1. Usecases and Lifecycle library
5.3.2. Lifecycle Events and Observers
5.3.3. LiveData
4
© 2022 e-Learning Centre, UCSC
5.1. Introduction to Android Architecture Components
Architecture Components
A set of Android libraries for structuring your app in a way that is
robust, testable, and maintainable.
• Consist of best architecture practices and libraries
• Encourage recommended app architecture
• A LOT LESS boilerplate code
• Testable because of clear separation
• Fewer dependencies
• Easier to maintain
5
© 2022 e-Learning Centre, UCSC
Overview
6
© 2022 e-Learning Centre, UCSC
The RoomWordsSample app
that you build in the practical
implements this architecture
7
© 2022 e-Learning Centre, UCSC
5.1.2. ViewModel
8
© 2022 e-Learning Centre, UCSC
Survives configuration changes
9
© 2022 e-Learning Centre, UCSC
ViewModel serves data
10
© 2022 e-Learning Centre, UCSC
Restaurant analogy
11
© 2022 e-Learning Centre, UCSC
5.1.3. Repository
12
© 2022 e-Learning Centre, UCSC
Multiple backends
13
© 2022 e-Learning Centre, UCSC
5.2. Room Database
14
© 2022 e-Learning Centre, UCSC
5.2.2. Components of Room
Entity:
Defines schema of database table.
DAO: Database Access Object
Defines read/write operations for database.
Database:
A database holder.
Used to create or connect to database
15
© 2022 e-Learning Centre, UCSC
5.2.2.1. Entity
16
© 2022 e-Learning Centre, UCSC
Entity instance = row in a database table
17
© 2022 e-Learning Centre, UCSC
Annotate entities
@Entity
public class Person {
@PrimaryKey (autoGenerate=true)
private int uid;
@ColumnInfo(name = "first_name")
private String firstName;
@ColumnInfo(name = "last_name")
private String lastName;
18
© 2022 e-Learning Centre, UCSC
@Entity annotation
@Entity(tableName = "word_table")
• Each @Entity instance represents an entity/row in a table
• Specify the name of the table if different from class name
@PrimaryKey annotation
@PrimaryKey (autoGenerate=true)
• Entity class must have a field annotated as primary key
• You can auto-generate unique key for each entity
• See Defining data using Room entities
19
© 2022 e-Learning Centre, UCSC
@NonNull annotation
@NonNull
• Denotes that a parameter, field, or method
return value can never be null
• Use for mandatory fields
• Primary key must use @NonNull
@ColumnInfo annotation
@ColumnInfo(name = "first_name")
private String firstName;
@ColumnInfo(name = "last_name")
private String lastName;
• Specify column name if different from member variable name
20
© 2022 e-Learning Centre, UCSC
Getters, setters
• be public
OR
21
© 2022 e-Learning Centre, UCSC
Relationships
22
© 2022 e-Learning Centre, UCSC
5.2.2.2. Data access object (DAO)
23
© 2022 e-Learning Centre, UCSC
Example DAO
@Query("DELETE FROM word_table")
void deleteAll();
Example queries
@Query("DELETE FROM word_table")
void deleteAll();
@Query("SELECT * from word_table ORDER BY word ASC")
List<Word> getAllWords();
@Query("SELECT * FROM word_table WHERE word LIKE :word ")
public List<Word> findWord(String word);
24
© 2022 e-Learning Centre, UCSC
5.2.2.3. Database
25
© 2022 e-Learning Centre, UCSC
Creating Room database
References
https://developer.android.com/reference/android/arch/persistence
/room/Database
26
© 2022 e-Learning Centre, UCSC
Room class example
27
© 2022 e-Learning Centre, UCSC
5.3. Lifecycle-aware Components
28
© 2022 e-Learning Centre, UCSC
5.3.1. Usecases and Lifecycle library
Use cases
• Switch between coarse and fine-grained location updates
depending on app visibility
• Stop and start video buffering
• Stop network connectivity when app is in background
• Pause and resume animated drawables
Lifecycle library
• Import the android.arch.lifecycle package
• Provides classes and interfaces that let you build lifecycle-aware
components that automatically adjust their behavior based on
lifecycle state of activity or fragment
• See Handling Lifecycles with Lifecycle-Aware Components
29
© 2022 e-Learning Centre, UCSC
5.3.2. Lifecycle Events and Observers
LifecycleObserver interface
@OnLifecycleEvent
30
© 2022 e-Learning Centre, UCSC
5.3.3. LiveData
31
© 2022 e-Learning Centre, UCSC
Use LiveData to keep UI up to date
Creating LiveData
32
© 2022 e-Learning Centre, UCSC
Using LiveData with Room
Room generates
all the code to
update the
LiveData when the
database is
updated
33
© 2022 e-Learning Centre, UCSC
Passing LiveData through layers
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
34
© 2022 e-Learning Centre, UCSC
Passing LiveData through layers
•DAO:
@Query("SELECT * from word_table")
LiveData<List<Word>> getAllWords();
•Repository:
LiveData<List<Word>> mAllWords =
mWordDao.getAllWords();
•ViewModel:
LiveData<List<Word>> mAllWords =
mRepository.getAllWords();
35
© 2022 e-Learning Centre, UCSC
Observing LiveData
No memory leaks
36
© 2022 e-Learning Centre, UCSC
LiveData is always up to date
• Example: an activity in
the background gets the
latest data right after it
returns to the
foreground
37
© 2022 e-Learning Centre, UCSC
LiveData handles configuration changes
If an activity or fragment is
re-created due to a
configuration change such as
device rotation, the activity or
fragment immediately
receives the latest available
data
38
© 2022 e-Learning Centre, UCSC
Share resources
•You can extend a LiveData object using the singleton pattern, for
example for services or a database
•The LiveData object connects to the system service once, and
then any observer that needs the resource can just watch the
LiveData object
•See Extend LiveData