Room Persistence Library in Android Jetpack PDF
Document Details
Uploaded by FastPacedJasper2846
Universiti Kebangsaan Malaysia
Tags
Summary
This document provides a comprehensive introduction to the Room persistence library in Android Jetpack. It covers the basic concepts of persisting data within Android apps, emphasizing simplified SQLite database interactions and abstraction layers.
Full Transcript
Room Persisting Data with Room Why Persist Data? Jetpack Example use cases: to-do lists, expense tracking, cataloging. Compose What is Room? Room Room is a persistence library in Android Jetpack. Simplifies SQLi...
Room Persisting Data with Room Why Persist Data? Jetpack Example use cases: to-do lists, expense tracking, cataloging. Compose What is Room? Room Room is a persistence library in Android Jetpack. Simplifies SQLite with compile-time checks and abstraction layers. Android Jetpack Suite of Tools — Image Credits: [d.android.com](http://d.android.com) Add Room dependencies Compone nts of Room Room entities represent tables in your app's database. You use them to update the data stored in rows in tables and to create new rows for insertion. Room DAOs provide methods that your app uses to retrieve, update, insert, and delete data in the database. Room Database class is the database class that provides your app with instances of the DAOs associated with that database. ?? ?? ?? ?? 1 Nike 289.00 50 2 Adidas 230.00 20 3 New 260.00 30 balance … … … … Entity Key Features: Annotated with @Entity. For each Entity class, the app creates a database table to hold the items. Defines fields as columns DAO A custom interface that provides convenience methods for querying/retrieving, inserting, deleting, and updating the database. ? DAO ? Operations: Insert or add a new item. ? Update an existing item to update the name, price, and ? quantity. Get a specific item based on its primary key, id. ? Get all items so you can display them. Delete an entry in the database. create a RoomDatabase that uses your Entity and DAO. The database class defines the list of Database entities and DAOs. It provides your app with instances of the DAOs you define. In turn, the app can use the DAOs to retrieve data from the database as instances of the associated data entity objects. Implement the Repository Save Data - onSaveClick DAO – Save Data – insertItem() Inspect Database How to display the items on the database ? Using HomeUiState to store the state of the data 1) Read It is based on flow - a generic stream of data - allows the data to be saved and Data observed To read data – use itemsRepository.getAllItemsStream() 1) Read Read from the assigned Data database collect and update the UI state in the HomeScreen using 2) Display homeUiState Data How to edit/updat e the items on the database? Update the entity using Room How to delete the items on the database ? to create and interact with the SQLite database using the Room library. to create an entity, DAO, and database classes. Summar y to read and display entities from a SQLite database. to update and delete entities from a SQLite database How to edit the items on the database ? Lab https://developer.android.com/build/ agp-upgrade-assistant Lab Lab https://developer.android.com/build/ agp-upgrade-assistant Lab Next For Dr Nazatul’s class ( Wed 8-10 am and Thursday 1-3 pm will be postponed to another date) This week: Continue Lab 6 Demo Lab Evaluation 2 (Lab 2 and Lab 3):Done Demo Lab Evaluation 3 (Lab 4 and Lab 5): 9/12-12/12/2024 Thank you Lab Test: 16/12-19/12/2024 Demo Project II: 20/12 - 2/1/2024 (with test)