[英]RxSwift. CombineLatest. Not all observables emitted
Observable.combineLatest(...){...} contains several observables, but some of these observables were not emitted. Observable.combineLatest(...){...}包含几个可观察对象,但是没有发出一些这些可观察对象。
combineLatest emits only when all observables in this method were emitted. 只有在发出此方法中的所有可观察对象时,combineLatest才会发出。
How to skip not emitted observables and emit combineLatest? 如何跳过不发出的observable并发出combineLatest?
let tap = firstButton.rx.tap.asObservable().map{ (_) -> Observable<Item> ...}
let textfieldObservable = viewTextField.rx.text.orEmpty.asObservable()
submitButton.rx.tap.withLatestFrom(Observable.combineLatest(textfieldObservable, tap ... )).flatMapLatest({
...
// this method will not be executed without tap on firstButton before tapping on submitButton
}
)
combineLatest
uses a closure that takes in as many arguments as it combines observables. combineLatest
使用一个闭包,它接收尽可能多的参数,因为它结合了observables。 So it makes sense it will wait for all the observables it combines to provide a value before it calls its closure. 所以它有意义等待它所结合的所有可观察对象在它调用它的闭包之前提供一个值。
But if you can find a sain default values for each of the observables provided to combineLatest
, you could use startWith(_:)
to force them into having an initial value. 但是如果你能找到为
combineLatest
提供的每个observable的sain默认值,你可以使用startWith(_:)
强制它们具有初始值。
This is what the code would look like using nil
for item
and the empty string for text
这就是代码看起来像使用
nil
作为item
和空字符串的text
let tapObservable: Observable<Item> = // ...
let textField: Observable<String> = // ...
let combined = Observable.combineLatest(
tapObservable.map { /* map everything to optional */ Optional.some($0) }.startWith(nil),
textField.startWith("")
) { item, text in
// combine item and text
}
submitButton.rx.tap.withLatestFrom(combined)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.