May 2022
Intermediate to advanced
688 pages
20h 12m
English
Now that CrimeRepository is set up to pull data from a populated database, update CrimeListViewModel to access the database when loading crimes instead of using the dummy data.
Listing 12.23 Accessing your database (CrimeListViewModel.kt)
class CrimeListViewModel : ViewModel() {
private val crimeRepository = CrimeRepository.get()
val crimes = mutableListOf<Crime>()
...
suspend fun loadCrimes(): List<Crime> {
val result = mutableListOf<Crime>()
delay(5000)
for (i in 0 until 100) {
val crime = Crime(
id = UUID.randomUUID(),
title = "Crime #$i",
date = Date(),
isSolved = i % 2 == 0
)
result += crime
}
return result
return crimeRepository.getCrimes()
}
}
Run the app and see the prepopulated crimes display ...
Read now
Unlock full access