[英]Which RxSwift operator to use for uniquely emitted elements and how to?
[英]iOS RxSwift - how to use Amb Operator?
我正在寻找RxSwift的Amb()运算符的例子。
我有两个observables - 启动和停止,我想创建代码,如果其中一个发出,将执行,而忽略来自另一个的发射。
let started = viewModel.networkAPI.filter { result in
switch result {
case .success:
return true
case .failure:
return false
}
}
let stopped = viewModel.networkAPI.filter { result in
switch result {
case .success:
return true
case .failure:
return false
}
}
我试过了:
Observable<Bool>.amb([started, stopped])
//error - Ambiguous reference to member 'amb'
//Found this candidate (RxSwift.ObservableType)
//Found this candidate (RxSwift.ObservableType)
和:
started.amb(stopped)
//error - Generic parameter 'O2' could not be inferred
如何正确使用RxSwift AMB运算符选择两个可观察量中的一个来首先发出值?
问题不在您发布的代码中。 我的猜测是你的Result
类型是通用的,并且已启动和已停止的observable正在过滤掉不同类型的Result。 从本质上讲,启动和停止具有不同的类型,因此, amb
不会与它们一起使用。
您可以通过执行let foo = [started, stopped]
然后测试foo
的类型来测试它。 您可能会收到错误:
异构集合文字只能推断为'[Any]'; 如果这是故意的,请添加显式类型注释
//convert both to the same emission type,
//then apply .amb after one of them
let stoppedBoolType = stopped.map { _ -> Bool in
return false
}
started.map{ _ -> Bool in
return true
}.amb(stoppedBoolType)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.