从单个视图创建多个可观察对象-RxSwift

时间:2018-11-19 15:03:51

标签: ios observable rx-swift

我的操作相对昂贵,因此我愿意执行一次该操作并从中创建2个Observables

这是它的外观:

let outputObservable1: Observable<Bool>
let outputObservable2: Observable<Bool>

(outputObservable1, outputObservable2) = inputObservable1.zip(inputObservable2).map { booleanCondition1, booleanCondition2 in
   // different condition combinations create different outputObservables
}

我猜这里map不是正确的运算符,因为它只会产生一个可观察的值。如何混合和匹配条件并立即返回2 Observables

1 个答案:

答案 0 :(得分:1)

根据我的理解,您只需要使用map

let inputs = Observable.zip(inputObservable1, inputObservable2)
    .share() // you only need this if one of your inputs is making a network request.

let outputObservable1 = inputs
    .map { first, second in 
        return true // or false depending on the values of first & second.
    }
let outputObservable2 = inputs
    .map { first, second in 
        return true // or false depending on the values of first & second.
    }