What is Room Persistence Library? Room is a library built on top of SQLite that provides a more convenient and structured way to manage your Android app's local database,lets you work with your data as Kotlin objects (data classes) instead of writing raw SQL queries this simplifies database interactions and also checks your SQL queries at compile time. This helps us to catch errors early in the development cycle. so kets get started. Dependencies dependencies { // Room implementation("androidx.room:room-runtime:2.5.0") implementation("androidx.room:room-ktx:2.5.0") kapt("androidx.room:room-compiler:2.5.0") } Key Components Entity @Entity(tableName = "word_table") data class Word( @PrimaryKey val word: String ) Entity is a Kotlin data class representing a single table in the database,each property of the class corresponds to a column in the table and each entity is annotates with
While building apps i have often incorporate ViewModels without fully grasping their intricacies. I made some common mistakes, like creating ViewModel instances within a remember block and such silly mistakes,but this journey has prompted me to dig a little deeper into understanding ViewModels and their internal workings. The primary questions we are going to address are as follows: What is a ViewModel? How to create a ViewModel? What is the difference between ViewModel and AndroidViewModel? What is a ViewModelFactory? What is viewModelScope? When is the ViewModel dropped by the ViewModelStore? What is onCleared() in the ViewModel ? lets begin our journey.... What is a ViewModel? ViewModel is a simple class that is part of the Android Architecture Components and its primary purpose is to store and manage UI-related data in a lifecycle-conscious manner caching its value during the lifecycle of the activity It helps us to avoid loading dat