Would it be possible to rewrite this in a way that...
# coroutines
f
Would it be possible to rewrite this in a way that
Loading
keeps emitting database updates rather than only a single result (
first
)?
Copy code
flow {
    val data = query().first()

    val flow = if (shouldFetch(data)) {
        emit(Resource.Loading(data))

        try {
            saveFetchResult(fetch())
            onFetchSuccess()
            query().map { Resource.Success(it) }
        } catch (t: Throwable) {
            onFetchFailed(t)
            query().map { Resource.Error(t, it) }
        }
    } else {
        query().map { Resource.Success(it) }
    }
    emitAll(flow)
}
d
What are expecting the result to look like, if you
println
all the emissions? (I'm not sure I understand the what you're trying to achieve)
f
this is a NetworkBoundResource, i.e. it mediates between cached and remote data
the way its implemented right now Resource.Loading doesn't carry any database updates
d
Ahhh, but database updates don't work until after the save I'm guessing.
Will there be any database updates except
saveFetchResult
?
f
yes, over the UI I can bookmark items which updates them in the DB