Lilly
07/23/2022, 6:30 PMflow of T
if cache is empty, otherwise T (as flow). I'm wondering which way is more appropriate:
fun fetchStuff(): Flow<Stuff> =
if (cache.isNotEmpty()) {
cache.asFlow()
} else {
api.fetchStuff()
}.map { model -> model.toStuffModel() }
or
fun fetchStuff(): Flow<Stuff> = flow {
if (cache.isNotEmpty()) {
emit(cache.first())
} else {
emitAll(api.fetchStuff())
}
}.map { model -> model.toStuffModel() }
Rob
07/24/2022, 2:29 AMLilly
07/24/2022, 2:52 AMJoffrey
07/24/2022, 8:37 AMapi.fetchStuff()
return? Suspend functions are more appropriate and easier to work with than flows of 1 element. It depends on whether your flows here have only one element, though. It is unclear from the code.if
expression extracted into a separate functionRob
07/24/2022, 3:14 PMsuspend fun fetchStuff(): Stuff {
val model = if (cache.isNotEmpty()) cache else api.fetchStuff()
return model.toStuffModel()
}
Lilly
07/28/2022, 8:44 AM