How to chain "Single - Completeable - Completable" in rxkotlin?

Viewed 1815

I am a beginner with rxjava/rxkotlin/rxandroid.

I need to deal with three diferent async-calls in a sequence. The problem is that the first step returns a Single<LocationResult>, the second a Completableand the third again a Completable.

(Single -> Completable -> Completable)

The problem is now that the last Completable depends on the data of the first Single

My current solution:

I think this is a bad solution, but I don't know how to do this right.

val ft = FenceTransaction(applicationContext, apiClient)
        stream
            .flatMap { locationResult ->
                ft.removeAll()
                return@flatMap ft.commit().toSingle({ return@toSingle locationResult })
            }
            .flatMapCompletable {
                ft.recycle()
                ft.setZone(it.location.longitude, it.location.latitude, ZONE_RADIUS)
                val dots = DotFilter().getFilteredDots()
                for (dot in dots) {
                    ft.addDot(dot)
                }
                return@flatMapCompletable ft.commit()
            }
            .subscribeBy(
                onComplete = {
                    "transaction complete".logi(this)
                },
                onError = {
                    "transaction error".logi(this)
                })

Is this approch the correct way to do it?

And how should I dispose the Completeables? Generally when should I dispose Observables?

1 Answers

No idea if you still have this issue but generally for Single->Completable->Completable you'd do:

val disposable = service.getSingleResult()
                        .flatMapCompletable { locationResult ->
                            callReturningCompletable(locationResult)
                        }
                        .andThen { finalCompletableCall() }
                        .subscribe({...}, {...})
Related