Introduction:
Android Kotlin application programming, particularly with a focus on the Room Persistent Library, is an important aspect of Android development.
In constrast to the very simple JSON approach of {KEY:Value} pairs in Shared Preferences, now we see how to use the Data Handling Framework of ROOM Framework:
Room Persistence Framework is a Wrapper (Mediator) set of APIs to give us connection to the SQLITE database built into Android OS to enable CRUD: Create Read Update Delete records in a SQL format.
Goal is the Persistence of User Data: Shared Preferences vs SQLite
When it comes to persisting user data in an Android app, there are two commonly used approaches: Shared Preferences and SQLite.
Here's how each approach persists data:
Shared Preferences:
Shared Preferences is a lightweight storage option for small amounts of data, typically used for storing user preferences or simple key-value pairs. Data stored in Shared Preferences is stored in a private partition accessible only by the application that created it, ensuring security. Shared Preferences data persists across user sessions, even if the app is stopped, restarted, or if the device is rebooted. It is represented by a small number of key-value pairs and is commonly used for storing user preferences that should be remembered across sessions, such as a user's preferred settings or their game score. However, Shared Preferences is not suitable for storing large or complex datasets. Shared Preferences is built into the Android Kotlin operating system. It is an Android API that allows activities or applications to store and retrieve data in the form of key-value pairs. The data stored in Shared Preferences remains persistent even if the app is closed, until it is deleted or cleared. The Android system uses Shared Preferences to store app settings data in the form of an XML file under the data/data/{application package}/share_prefs directory.
To access Shared Preferences in an application, you need to get an instance of it using methods such as getSharedPreferences() or getPreferences(). The modifications to the preferences data are performed through the SharedPreferences.Editor object. You can also delete the preferences data of an application using the appropriate method.
Shared Preferences is commonly used for storing small amounts of primitive data, such as user preferences or simple key-value pairs. It provides a convenient way to store and retrieve data without the need for a full-fledged database like SQLite.
SQLite: SQL is what we need for more data heavy applications requiring build a Data Model.
(Expert practioner’s Note: Look for ways to push as much as possible of the algorithm processing in your data persistence layer such as the SQL. Should be able to push about 70% of your algorithm execution into the Data Container).
SQLite is a relational database management system that provides a more robust and structured approach to data storage. Implements the ANSI 77 Implementation of SQL. It is an implementation of the SQL language and is used for storing structured data in a private database. SQLite databases are stored as files on the device's file system and can be accessed by the application that created them. SQLite is suitable for scenarios where there is a large amount of data to be stored, and where more complex querying and manipulation (and data modelling) of data is required. It provides the ability to perform advanced database operations, such as filtering, sorting, and joining data. Room, which is part of the Android Development Framework called Android Jetpack, is an abstraction layer over SQLite that simplifies database setup, configuration, and interactions with the app. ROOM is a FRAMEWORK which a set of APIs that deliver the low level implementation details of CRUD.
In summary, Shared Preferences is ideal for storing small amounts of simple data, such as user preferences, while SQLite is better suited for larger and more complex datasets that require structured storage and advanced querying capabilities.
Android Developer Library for Room
The Android Developer Library provides comprehensive documentation and resources for Room, which is a part of Android Jetpack and serves as an abstraction layer over SQLite.
Room allows for more robust database access while harnessing the full power of SQLite.
Here are some useful links to the Android Developer Library for Room:
This article provides an introduction to Android Jetpack and explains how Room is a part of it. It also highlights the benefits of using Room for database access. This official Android Developers guide explains how to save data in a local database using Room. It covers topics such as caching data, compile-time verification of SQL queries, and streamlined database migration paths. This page provides information about Room, including the latest version, release notes, and dependencies required to use Room in your app. This page provides information about SQLite, which is the underlying database technology used by Room. It also mentions that Room is an abstraction layer over SQLite and provides more robust database access. This article provides a basic implementation of Room Database with Repository and ViewModel. It explains the advantages of using Room, such as compile-time verification of SQL queries, less boilerplate code, and easy integration with other architecture components. This article explains how to use Room Database with MVVM Architecture and Kotlin Coroutines. It provides a step-by-step guide to creating a Note App using Room for CRUD operations. This article provides a tutorial on implementing Room Database in an Android application. It covers topics such as creating a new project, adding Room dependencies, and setting up the database. This official Android Developers guide explains how to persist data with Room. It provides an overview of Room's features, such as simplified database setup, configuration, and compile-time checks of SQLite statements. This tutorial provides an example of building a Todo App using Room Persistence Library. It covers topics such as the components of Room, creating a database, and performing CRUD operations. This article provides an introduction to Room Persistent Library in Android. It explains how Room serves as an abstraction layer over SQLite and simplifies database access.
Room is a part of Android Jetpack and is an abstraction layer over SQLite. It allows for more robust database access while harnessing the full power of SQLite.
### Understanding Room Persistent Library
#### Key Features:
1. **Compile-Time Verification:** Room verifies SQL queries at compile time. This means errors are caught earlier in the development process, making your database code more robust and stable.
2. **Boilerplate Code Reduction:** Room minimizes the amount of boilerplate code required to interact with the database, as compared to raw SQLite.
3. **Integration with LiveData and RxJava:** Room works well with other architecture components like LiveData and RxJava, facilitating easy data observation and stream management.
4. **Easy Database Migration:** Room provides support for database migrations with less effort.
5. **Annotations-Based:** Developers use annotations like `@Entity`, `@Dao`, and `@Database` to define tables, database access objects (DAOs), and databases.
#### Components:
1. **Entity:** Represents a table within the database. Annotated with `@Entity`.
2. **DAO KOTLIN OBJECT (Data Access Object):** Provides the methods that your app uses to query, update, insert, and delete data in your database.
You put methods in your DAO corresponding to create / read / update / delete of records in your Tables (ENTITIES).
3. **Database:** An abstraction class that holds the database and serves as the main access point for the underlying connection to your app's persisted data.
### Persistence in Local Storage - Traditional Methods
Traditionally, persisting data to local storage in Android could be done using:
1. **Shared Preferences:** Ideal for storing small amounts of data in key-value pairs. Not suitable for complex data structures.
2. **SQLite Database:** Provides a structured database environment but requires a significant amount of boilerplate code for setup, queries, and updates.
3. **File Storage:** Saving data directly to the device's file system. Useful for text, binary data, or even object serialization.
How Room Differs from Traditional Local Storage Methods
1. **Abstraction Over SQLite:** Room provides a higher level of abstraction over SQLite, simplifying database operations.
2. **Compile-Time Safety:** SQL queries are checked at compile time in Room, reducing runtime errors.
3. **Less Boilerplate:** Room significantly reduces the amount of boilerplate code, especially compared to raw SQLite usage.
4. **Integration with Modern Tools:** Room's integration with LiveData, RxJava, and Kotlin Coroutines makes it more suited for modern Android development with efficient data handling.
5. **Migration Support:** Room provides an easier path for database migrations and schema updates.
6. **Annotation-Driven:**
Room uses annotations for database entities and operations, making the code more declarative and easier to understand.
In summary, while traditional methods like SharedPreferences and raw SQLite are still valid, Room provides a more sophisticated and efficient way to interact with SQLite databases, especially suitable for larger, more complex applications where database integrity, ease of maintenance, and scalability are important.
The big win with ROOM is support for constructing complex data modeling to model the business objects in your Business Domain.