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

Architecture Componets

Architecture Componets

Introduction to Android Architecture Components, i.e. ViewModel, LiveData and Room.

Beatrice Kinya

December 02, 2021
Tweet

More Decks by Beatrice Kinya

Other Decks in Technology

Transcript

  1. Introduction ❖ App architecture design is an important consideration for

    ensuring that your apps are robust, testable, and maintainable. ❖ Android provides a set of libraries and components, such as ViewModel, Livedata, Room, to help you put together your app according to best practices. ❖ Architecture principles are ➢ Separation of concerns ➢ Drive your UI from a model
  2. Separation of concerns ❖ The UI classes, i.e. activities or

    fragments, should only contain logic that handle UI and operating system interactions such as displaying data to user, permission request etc. ❖ By keeping UI classes lean, you can avoid many life-cycle related problems . ❖ Example: Configuration changes
  3. Drive the UI from a model ❖ You should drive

    your UI from a persistent model. ❖ This way ➢ Your app continues to work in cases of no network or users network is flaky. ➢ Models are unaffected by apps lifecycle. This way the user does not lose data in case OS destroys your app to clear up resources.
  4. ViewModel ❖ It contains data specific to a UI component,

    such as a fragment or an activity. ❖ It contains data handling logic to communicate with the model.
  5. The Lifecycle of A ViewModel ❖ It allows data to

    survive configuration changes such as screen rotation. ❖ ViewModel is scoped within the Lifecycle of the related UI component such as an Activity or a Fragment. ❖ The ViewModel remains in memory until the Lifecycle it is scoped goes away; the activity is destroyed or fragment detached. .
  6. Livedata ❖ It is an observable data holder class. ➢

    Live data updates can be observed by other components such as activities or fragments. ❖ It is lifecycle aware. ➢ It respects the lifecycle of other components such as activities or fragments. ➢ It notifies app components observers that are in an active lifecycle state about updates.
  7. Observers, LifeCycleOwner ❖ Livedata considers an observer(Activity or Fragment )

    to be in active state if its lifecycle is in STARTED or RESUMED state. ❖ You can register an observer paired with an object that implements LifecycleOwner interface, i.e. Activity or a Fragment. ➢ This relationships allows observers to be removed when the state of the corresponding object changes to DESTROYED.
  8. Room ❖ The Room persistence library provides an abstraction layer

    over SQLite to allow for more fluent database access. ❖ Room has three major components. ➢ The Database class ➢ Data entities ➢ Data Access Objects(DAOs)
  9. Data entities ❖ Represent tables in the app database. ❖

    Each entity is a data class that is annotated with @Entity. ❖ Each property in the class represents a column in the table. ❖ The following is an example of Student entity. @Entity(tableName = "student") data class Student( @PrimaryKey val id: Int, val name: String, val course: String )
  10. Data Access Objects(DAOs) ❖ Annotated with @Dao. ❖ They include

    methods that offer access to data stored in the app database. ❖ You can define a DAO either as an interface or an abstract class. For basic use cases you should use an interface.
  11. An example, StudentDao @Dao interface StudentDao{ @Insert suspend fun saveStudents(students:

    List<Student>) @Query("SELECT * FROM student") suspend fun getAllStudents(): List<Student> }
  12. DAO methods ❖ There are two types of Dao methods

    ➢ Convenience methods that let you insert, update, and delete rows in your database without writing any SQL code. ➢ Query methods that let you write your own SQL query to interact with the database.
  13. Convenience Methods ❖ Room provides convenience annotations for defining methods

    that perform simple inserts, updates, and deletes without requiring you to write a SQL statement. ❖ @Insert: annotation allows you to define methods that insert their parameters into the appropriate table in the database. @Insert fun saveStudents(students: List<Student>)
  14. Convenience methods cont’ ❖ @Update annotation allows you to define

    methods that update specific rows in a database table. @Update fun updateStudent(student: Student)
  15. Convenience methods cont’ ❖ @Delete annotation allows you to define

    methods that delete specific rows from a database table. @Delete fun deleteStudent(student: Student) ❖ @Insert, @Update and @delete methods accept data entity instances as parameters. ❖ If you need to define more complex inserts, updates, or deletes, or if you need to query the data in the database, use a query method instead.
  16. Query Method ❖ @Query annotations allows you to write SQL

    statements and expose them as DAO methods. @Query("SELECT * FROM student") fun getAllStudents(): List<Student> ❖ Use query methods to query data from your app's database, or when you need to perform more complex inserts, updates, and deletes.
  17. Database class ❖ It is an abstract class that extends

    RoomDatabase. ❖ It is annotated with @Database. ❖ It holds the database. It defines the database configurations, and serves as main access point to the app’s persistent data. ➢ It contains entities(tables) associated with the database and the database version number.