The Monster
09/09/2021, 7:18 PMgetUser
function is not suspend, but it is calling refreshUser
function. The code is from here https://developer.android.com/jetpack/guide#persist-data
fun getUser(userId: String): Flow<User> {
refreshUser(userId)
// Returns a Flow object directly from the database.
return userDao.load(userId)
}
private suspend fun refreshUser(userId: String) {
// Check if user data was fetched recently.
val userExists = userDao.hasUser(FRESH_TIMEOUT)
if (!userExists) {
// Refreshes the data.
val response = webservice.getUser(userId)
// Check for errors here.
`// Updates the database. Since userDao.load()
returns an object of`
`// Flow<User>
, a new User
object is emitted every time there's a`
`// change in the User
table.`
userDao.save(response.body()!!)
}
}
Joffrey
09/09/2021, 7:33 PMThe Monster
09/09/2021, 7:40 PMAdam Powell
09/09/2021, 8:28 PMAdam Powell
09/09/2021, 8:31 PM