简体   繁体   中英

How can RxJava2's onErrorResumeNext counterpart in Kotlin Flow that returns another flow be implemented?

I want to implement offline-last approach with Flow , first try to fetch data from remote source if it fails, for instance Retrofit throwing network exception, i want to fetch data from local source with the code below

    return flow { emit(repository.fetchEntitiesFromRemote()) }
        .map {

            println("🍏 getPostFlowOfflineLast() First map in thread: ${Thread.currentThread().name}")

            val data = if (it.isEmpty()) {
                repository.getPostEntitiesFromLocal()
            } else {
                repository.deletePostEntities()
                repository.savePostEntity(it)
                repository.getPostEntitiesFromLocal()
            }

            entityToPostMapper.map(data)
        }
        .catch { cause ->
            println("❌ getPostFlowOfflineLast() FIRST catch with error: $cause, in thread: ${Thread.currentThread().name}")
           flow { emit(repository.getPostEntitiesFromLocal()) }
        }
        .map { postList ->

            println("🎃 getPostFlowOfflineLast() Second map in thread: ${Thread.currentThread().name}")

            ViewState<List<Post>>(
                status = Status.SUCCESS,
                data = postList
            )
        }
        .catch { cause: Throwable ->

            println("❌ getPostFlowOfflineLast() SECOND catch with error: $cause, in thread: ${Thread.currentThread().name}")

            flow {
                emit(
                    ViewState<List<Post>>(
                        Status.ERROR,
                        error = cause
                    )
                )
            }
        }

But it gets stuck with exception

I: ❌ getPostFlowOfflineLast() FIRST catch with error: java.net.UnknownHostException: Unable to resolve host "jsonplaceholder.typicode.com": No address associated with hostname, in thread: main

What should be the right implementation to have any observable like with RxJava onResumeNext if repository function was an Observerable?

onErrorResumeNext { _: Throwable ->
     Observable.just(repository.getPostEntitiesFromLocal())
}

Figured out that i can use emitAll with a flow to continue flow even multiple times.

    .catch { cause ->
        println("❌ getPostFlowOfflineLast() FIRST catch with error: $cause, in thread: ${Thread.currentThread().name}")
        emitAll(flow { emit(repository.getPostEntitiesFromLocal()) })
    }
    .map {
        if (!it.isNullOrEmpty()) {
            entityToPostMapper.map(it)
        } else {
            throw EmptyDataException("No data is available!")
        }
    }
    .map { postList ->
        println("🎃 getPostFlowOfflineLast() Third map in thread: ${Thread.currentThread().name}")
        ViewState(status = Status.SUCCESS, data = postList)
    }
    .catch { cause: Throwable ->
        println("❌ getPostFlowOfflineLast() SECOND catch with error: $cause, in thread: ${Thread.currentThread().name}")
        emitAll(flow { emit(ViewState(Status.ERROR, error = cause)) })
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM