https://kotlinlang.org logo
#coroutines
Title
# coroutines
f

Florian

01/30/2021, 10:46 AM
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

Dominaezzz

01/30/2021, 1:44 PM
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

Florian

01/30/2021, 3:48 PM
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

Dominaezzz

01/30/2021, 4:13 PM
Ahhh, but database updates don't work until after the save I'm guessing.
Will there be any database updates except
saveFetchResult
?
f

Florian

01/30/2021, 7:09 PM
yes, over the UI I can bookmark items which updates them in the DB
2 Views