Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift: Extra argument 'onError' in call

I have following code in Swift 4 with RxSwift

worthReacting.flatMap{ (userSearch) in
        translator.getTranslation(ofWord: userSearch)
        }.subscribe(
            onSuccess: {(dataModel) in
                state.value = .translation(word: dataModel.definition,
                                           translations: dataModel.translations)
        },
            onError: {(error) in
                state.value = .networkError
        },
            onCompleted: {
                state.value = .unknownWord
        }).disposed(by: disposeBag)

worthReacting has type of Observable<String>
translator.getTranslation returns Maybe<DataModel>

I'm getting build error

Extra argument 'onError' in call

like image 235
Alexey Shvachka Avatar asked Nov 11 '17 02:11

Alexey Shvachka


2 Answers

Maybe flatmaped into Observable produces Observable. Observable can not emit onSuccess event, instead it will emit onNext. Following code will work:

worthReacting.flatMap{ (userSearch) in
        translator.getTranslation(ofWord: userSearch)
        }.subscribe(
            onNext: {(dataModel) in
                self.state.value = .translation(word: dataModel.definition,
                                           translations: dataModel.translations)
        },
            onError: {(error) in
                self.state.value = .networkError
        },
            onCompleted: {
                self.state.value = .unknownWord
        }).disposed(by: disposeBag)
like image 72
Alexey Shvachka Avatar answered Oct 11 '22 11:10

Alexey Shvachka


For those who get the OP's error, but have a different cause, check that you have not incidentally make one of your closures throwing by not making your do-catch clause exhaustive.

Also, take care that you don't use a single-statement closure (in this case you should explicitly return).

like image 32
Jarrod Moldrich Avatar answered Oct 11 '22 12:10

Jarrod Moldrich