Tutorial 5
Save useful data locally.
Use DataStore for small preferences and Room for structured app data. Do not force everything into one storage mechanism.
Storage choice
| DataStore | Theme, language, onboarding seen, selected city, sort preference. |
|---|---|
| Room | Favourite businesses, cached catalogue rows, notes, records with ids. |
| Network only | Fresh data that can be re-fetched and does not need offline use. |
Room-style favourite entity
@Entity(tableName = "favourites")
data class FavouriteBusinessEntity(
@PrimaryKey val businessId: Int,
val savedAtMillis: Long
)
@Dao
interface FavouriteBusinessDao {
@Query("SELECT * FROM favourites")
suspend fun getAll(): List<FavouriteBusinessEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun save(entity: FavouriteBusinessEntity)
@Query("DELETE FROM favourites WHERE businessId = :id")
suspend fun remove(id: Int)
}Practice task
- Save selected city or language using DataStore.
- Save favourite business ids using Room or a simple local abstraction first.
- Show favourites after app restart.
- Write down why each value belongs in its chosen storage.