0

using kotlin, having code

fun fetchRemoteDataApi(): Single<RemoteDataResponse> = networkApi.getData()

    // it is just a retrofit
    @GET(".../api/getData")
    fun getData() : Single<RemoteDataResponse>

fun mergeApiWithDb(): Completable = fetchRemoteDataApi()
            .zipWith(localDao.getAll())
            .flatMapCompletable { (remoteData, localData) ->
                doMerge(remoteData, localData) //<== return a Completable
            }

the code flow:

    val mergeApiDbCall = mergeApiWithDb().onErrorComplete().cache() //<=== would like do some inspection at this level
    PublishSubject.create<Unit>().toFlowable(BackpressureStrategy.LATEST)
                .compose(Transformers.flowableIO())
                .switchMap {
                    //merge DB with api, or local default value first then listen to DB change
                    mergeApiDbCall.andThen(listAllTopics())
                            .concatMapSingle { topics -> remoteTopicUsers.map { topics to it } }
                }
                .flatMapCompletable { (topics, user) ->
                    // do something return Completable
                }
                .subscribe({
                    ...
                }, { throwable ->
                    ...
                })

and when making the call

val mergeApiDbCall = mergeApiWithDb().onErrorComplete().cache()

the question is if would like to inspect on the Singles<RemoteDataResponse> returned from fetchRemoteDataApi() (i.e. using Log.i(...) to printout the content of RemoteDataResponse, etc.), either in got error or success case, how to do it?

/// the functions
fun listAllTopics(): Flowable<List<String>> = localRepoDao.getAllTopics()

// which a DAO:
    @Query("SELECT topic FROM RemoteDataTable WHERE read = 1")
    fun getAllTopics(): Flowable<List<String>>

///
private val remoteTopicUsers: Single<List<User>>
        get() {
            return Single.create {
                networkApi.getTopicUsers(object : ICallback.IGetTopicUsersCallback {
                    override fun onSuccess(result: List<User>) = it.onSuccess(result)
                    override fun onError(errorCode: Int, errorMsg: String?) = it.onError(Exception(errorCode, errorMsg))
                })
            }
        }
lannyf
  • 9,865
  • 12
  • 70
  • 152

1 Answers1

1

You cannot extract information about elements from the Completable. Though you can use doOnComplete() on Completable, it will not provide you any information about the element.

You can inspect elements if you call doOnSuccess() on your Single, so you need to incorporate this call earlier in your code. To inspect errors you can use doOnError() on both Completable or Single.

Konstantin Raspopov
  • 1,565
  • 1
  • 14
  • 20