Based on my understanding of the NetworkBoundResource it looks like when the app will start for the first time, loading progress won't appear and the user will see a blank screen.
Here is why:
fun asFlow() = flow {
emit(ViewState.loading())
val dbValue = loadFromDb().first()
if (shouldFetch(dbValue)) {
emit(ViewState.success(dbValue))
try {
val apiResponse = fetchFromNetwork()
when {
apiResponse.isSuccessful && apiResponse.body() != null -> {
apiResponse.body()?.let { saveNetworkResult(it) }
emitAll(loadFromDb().map { ViewState.success(it) })
}
else -> {
emit(ViewState.error(apiResponse.message()))
}
}
} catch (e: Exception) {
emit(ViewState.error("Unable to fetch from network"))
}
} else {
emitAll(loadFromDb().map { ViewState.success(it) })
}
}
The shouldFetch() doesn't respect dbValue's actual value.
At the first start, this will be empty. However, code will immediately emit an Empty success state whereas it is actually still loading items from the network.
Based on my understanding of the
NetworkBoundResource
it looks like when the app will start for the first time, loading progress won't appear and the user will see a blank screen.Here is why:
The
shouldFetch()
doesn't respectdbValue
's actual value.At the first start, this will be empty. However, code will immediately
emit
anEmpty
success state whereas it is actually still loading items from the network.