I am developing a brand new android app and i wanted to implement an Offline-First strategy using Kotlin Flow just like we do it using RxJava. i use below code for the offline-first functionality when i use Rx.
private fun getOfflineFirst(param: Param): Observable<T> =
Observable.concatArrayEagerDelayError(
getOffline(param), getRemote(param)
getOffline and getRemote functions will return an observable object. and i use below code to achieve same result using Flow.
private suspend fun getOfflineFirst(param: Param) = flow {
getLocal(param)
.onCompletion {
getRemote(param).collect {
emit(it)
}
}.collect { emit(it) }
}
getLocal and getRemote will return a Flow object. also i use another logic in one of my playground projects like below:
suspend fun getResult(param: PARAM, strategy: QueryStrategy): Flow<ResultResponse> = flow {
if (strategy.isRemote()) {
emit(getRemoteResult(param))
} else {
emit(getLocalResult(param))
emit(getRemoteResult(param))
}
}
In the "else" section it's going to emit the local result then the remote, that's how i handled first-offline in this scenario.
But i am not sure if i am using the best approaches available.
Can someone suggest me some better approaches?