Can I raise an error from a map function or do I need to use flatMap? The error should be reported to onError. In RxJava I can simply throw an exception. How can I do the following (pseudo-)code
observable.map( value -> {if (value.isIllegal) raiseError else return value.count})
Obviously it is possible using a flatMap, but I am looking for a map solution.
The Map operator applies a closure to each item emitted by the source Observable, and returns an Observable that emits the results of applied callback.
RxSwift: Driver Go from: a single observable that updates the entire UI to bindTo and reuse the same observable across the viewController.
Works the same way in RxSwift -
enum MyError: Error {
case myError
}
Observable
.from([1,2,3])
.map { (element) -> Int in
throw MyError.myError
}
.subscribe { event in
os_log("%@", "\(event)")
}
.disposed(by: self.disposeBag)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With