Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I raise an Error from rxSwift map-Function

Tags:

swift

rx-swift

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.

like image 584
Benjamin Mesing Avatar asked Dec 15 '17 10:12

Benjamin Mesing


People also ask

What is MAP RxSwift?

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.

What is RxSwift driver?

RxSwift: Driver Go from: a single observable that updates the entire UI to bindTo and reuse the same observable across the viewController.


1 Answers

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)
like image 68
Maxim Volgin Avatar answered Nov 11 '22 11:11

Maxim Volgin