Posts

Showing posts with the label room

Room with LiveData, ViewModel - Android Architecture Components - Kotlin

Image
Introduction: We have already learnt about Room in last post,  Room-Kotlin . In this post, lets learn about how to use ViewModel and LiveData with Room to improve usablity. Using LiveData with Room, allows views to be notified about data changes automatically. LiveData: LiveData is nothing but observable data holder class. It allows us to observer changes in data across multiple components of app. It is also aware of and respects lifecycle of Activities/Fragments. It also ensures that LiveData only updates app component observers that are in an active lifecycle state. The following image depicts the Lifecycle and States: Here, CREATED, STARTED, RESUMED - Active States           INTIALIZED, DESTROYED - InActive States So, LiveData will remove the updates automatically while in InActive States, i.e., INTIALIZED and DESTROYED. ViewModel: ViewModel is an entity that is free of Activity/Fragment's lifecycle. So it allows us...

Android Architecture Components - Room - Kotlin

Image
Introduction: Google's Android Architecture components became stable now. It is nothing but a collection of libraries that ease our Android Development. Android Architecture Components: DataBinding Navigation Paging Library WorkManager LiveData & ViewModel Room Persistence Library Let's see about Room in this post.  Room is a new way to create a database in your Android Apps.  SQLite vs Room: The following are some of the advantages of Room over SQLite. (a) In SQLite, there is no compile-time verification of SQL queries, whereas Room verifies it. (b) We need to write lots of boilerplate codes to convert SQL queries to Java Objects. (c) We need to update the affected SQL queries manually, while the schema changes in SQLite. Whereas, Room will take care of it automatically. Room Architecure and Components: Three components are there in Room: (1) Database is a holder class that uses annotation to define the list of ...