tseisel
11/26/2019, 9:56 AMFlow
. I have a source flow the emits the current result of a request in database whenever it changes (like a Flow
returned by Android Room). I decided to expose the result of those request with a repository.
interface DataSource {
val flow: Flow<List<Foo>>
}
class Repository(
private val source: DataSource
)
val diff: Flow<Diff<Foo>> get() = TODO()
suspend fun getLatest(): List<Foo> = TODO()
}
How can I transform the source flow to adapt it to the needs of the Repository
? (The Diff
is calculated from the latest and the previous list of `Foo`s.)
Is it possible to cache the latest value just like Android LiveData
, to avoid costly database queries when repeateadly reading the latest values ?octylFractal
11/26/2019, 9:57 AMtseisel
11/26/2019, 12:02 PMconflate
operator may be part of the solution, but it does not cache the latest value, each call to getLatest()
still triggers a database querygildor
11/26/2019, 12:40 PMtseisel
11/26/2019, 3:55 PMDataFlow
rethrow that exception or re-collect the upstream ? I'd really like option 2.gildor
11/27/2019, 1:02 AMgildor
11/27/2019, 1:02 AMgildor
11/27/2019, 1:04 AM