[英]RxSwift - how to have readable combineLatest event element variable names?
var isCurrentlySearchingAddress = Variable<Bool>(false)
var currentSelectedPlace = Variable<GeocodePlace?>(nil)
when I make a subscription on 当我进行订阅时
Observable.combineLatest(viewModel.isCurrentlySearchingAddress.asObservable(), viewModel.currentSelectedPlace.asObservable())
How can I have readable variable names instead of 0
and 1
? 如何获得可读的变量名,而不是0
和1
?
If you are only interested in next
events you can do this: 如果您仅对next
事件感兴趣,可以执行以下操作:
Observable.combineLatest(isCurrentlySearchingAddress.asObservable(), currentSelectedPlace.asObservable())
.subscribe(onNext: { (isSearchingAddress, selectedPlace) in
})
Or this (if you want to keep using subscribe
): 或这个(如果您想继续使用subscribe
):
Observable.combineLatest(isCurrentlySearchingAddress.asObservable(), currentSelectedPlace.asObservable())
.subscribe { event in
if case let .next(isSearchingAddress, selectedPlace) = event {
}
}
You can do it like this: 您可以这样做:
Observable.combineLatest(
varA.asObservable(), varB.asObservable(),
resultSelector: { value1, value2 in
print("\(value1) \(value2)")
}).observeOn(MainScheduler.instance)
.subscribe()
.disposed(by: disposeBag)
Here you're using the subscribe(on:)
then force unwrapping element
. 在这里,您使用的是subscribe(on:)
然后强制展开element
。 In case of error the element
will we nil
and it will cause crash. 万一发生错误, element
将nil
,这将导致崩溃。 It is not a good idea to force unwrap it. 强制拆开它不是一个好主意。
If you're sure that the sequence will never emit error or you don't want to handle the error then it's better to use subscribe(onNext:)
like this: 如果您确定序列永远不会发出错误,或者您不想处理错误,那么最好使用subscribe(onNext:)
如下所示:
Observable.combineLatest(viewModel.isCurrentlySearchingAddress.asObservable(), viewModel.currentSelectedPlace.asObservable())
.subscribe(onNext: { (isCurrentlySearchingAddress, currentSelectedPlace) in
// Use isCurrentlySearchingAddress: Bool and currentSelectedPlace: GeocodePlace? here.
if (isCurrentlySearchingAddress) {
// TODO:
}
})
Also don't forget to add it to DisposeBag
. 同样不要忘记将其添加到DisposeBag
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.