RxJava Return single, execute completable after

前端 未结 4 546
一生所求
一生所求 2021-02-13 18:58

I\'m trying to accomplish the following: Return some data as single, execute a completable after. The following code does not compile due to single.andThen(). The actions need t

相关标签:
4条回答
  • 2021-02-13 19:42

    Note that if you don't care whether the result is Single or Completable there is a special flatMapCompletable operator in RxJava2 to execute completable after Single:

    single.flatMapCompletable(result -> completable);

    0 讨论(0)
  • 2021-02-13 20:00

    Use flatMap:

    single.flatMap(v -> completable.andThen(Single.just(v)))
    
    0 讨论(0)
  • 2021-02-13 20:02

    Assuming you actually want to return the Single after the Completable, here's another way:

    Using Java:

    single.flatMap(x -> completable.toSingleDefault(x))
    

    Using Kotlin:

    single.flatMap { completable.toSingleDefault(it) }
    
    0 讨论(0)
  • 2021-02-13 20:03

    If anyone is interested in a the RxSwift solution:

    saveObjectsA().flatMap { (objectsA: [A]) -> Single<Bool> in
            B.objects = objectsA
            return completable.andThen(Single.just(true))
        }
    

    saveObjectsA returns a Single<[A]> which is an attribute of B (created previously). I needed to save it before saving B.

    0 讨论(0)
提交回复
热议问题