Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift: Extra argument 'onError' when subscribing on an Observable<String>

Tags:

swift

rx-swift

I have the following code:

let fetcher = DiagnosticFetcher(commandSender: sender)
fetcher.fetch()
    .observeOn(MainScheduler.instance)
    .subscribe(
       onNext: { self.store.save(content: $0) },
       onError: { self.view.showError("Error") },
       onCompleted: { log.verbose("Diagnostic fetched") })

It does not compile: Extra argument 'onError' in call. I get the same error if I use onSuccess or onDoesNotExistButShowTheBug instead of onNext.

The fetch() method returns a Observable<String> (whose last operator is a reduce). It seems that the subscribe() call expects only one lambda:

fetcher.fetch()
   .observeOn(MainScheduler.instance)
   .subscribe(onNext: { self.store.save(content: $0) })

Results in: Extraneous argument label 'onNext:' in call. And:

fetcher.fetch()
   .observeOn(MainScheduler.instance)
   .subscribe({ self.store.save(content: $0) })

compiles fine.

I feel like I get the wrong subscribe() implementation. I want that one:

public func subscribe(onNext: ((ElementType) -> Void)? = nil,
                      onError: ((Swift.Error) -> Void)? = nil,
                      onCompleted: (() -> Void)? = nil) -> Disposable {

but obviously, the compiler doesn't. I'm using XCode 9.2 with Swift 4 and RxSwift 4.1.1.

I have other parts in my app that use the onNext:onError: on an observable where it works. I can't put my finger on what is different for this call.

Any thought on how I can identify the root of the issue?

like image 536
fstephany Avatar asked Jan 17 '18 12:01

fstephany


2 Answers

I got it to compile by specifying the first parameter in the onError lambda:

fetcher.fetch()
   .observeOn(MainScheduler.instance)
   .subscribe(
      onNext: { self.store.save(content: $0) },
      onError: { _ in self.view.showError("Error")})
like image 163
fstephany Avatar answered Nov 15 '22 07:11

fstephany


Adding my contribution.

I had the same issue but, in my case:

recordHeader.albumArray.asObservable()
    .subscribe(onNext: { [weak self] value in
        self?.populateView(recordHeader: value)
    })
    .disposed(by: disposeBag)

The value type of the function "populateView" didn't match to value type of value

like image 28
Reimond Hill Avatar answered Nov 15 '22 07:11

Reimond Hill