I'm looking for example of Amb() operator for RxSwift.
I have two observables - started and stopped and I want to create code which would execute if one of them emits, while ignoring emissions from the other one.
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
}
}
I tried:
Observable<Bool>.amb([started, stopped])
//error - Ambiguous reference to member 'amb'
//Found this candidate (RxSwift.ObservableType)
//Found this candidate (RxSwift.ObservableType)
and:
started.amb(stopped)
//error - Generic parameter 'O2' could not be inferred
How do I properly use RxSwift AMB operator to pick one of two observables to emit a value first?
The problem isn't in the code you posted. My guess is that your Result
type is generic and the started and stopped observables are filtering off of different types of Result. In essence, started and stopped have different types and therefore, amb
won't work with them.
You can test this by doing let foo = [started, stopped]
and then testing the type of foo
. You will probably get the error:
Heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional
//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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.