0

Following is my code for signing up

self.signedUp  = signUpButtonTap.withLatestFrom(userAndPassword).flatMapLatest{
            input ->  Observable<Response> in
            return  Observable.create { observer in
                let userData = Creator()
                userData?.username = input.0
                userData?.password = input.1
                provider.request(.signIn(userData!)).filter(statusCode: 200).subscribe{  event -> Void in

                    switch event {
                    case .next(let response):
                        observer.onNext(response)

                    case .error(let error):
                        let moyaError: MoyaError? = error as? MoyaError
                        let response: Response? = moyaError?.response
                        let statusCode: Int? = response?.statusCode
                        observer.onError(error)

                    default:
                        break
                    }

                }
                return Disposables.create()
            }
        }

Following is the binding in the View

 self.viewModel.signedUp.bind{response in
               self.displayPopUpForSuccessfulLogin()
            }

When there is a successful response its works fine.

But when the request times out or I get any other status code than 200, I get the following error "fatalError(lastMessage)" and the app crashes.

When I replace observer.onError(error) with observer.onNext(response) in the case .error it works for response codes other than 200 , but crashes again when the request times out.

I have gone through this link Handling Network error in combination with binding to tableView (Moya, RxSwift, RxCocoa)

Can anyone help me out with what is wrong. I am completely new to RxSwift . Any help will be appreciated. Thank you

A.S
  • 798
  • 1
  • 10
  • 32
  • Could you please try to return your `subscribe` instead of `Disposables.create()`? Just place a return before `provider...`. Does it solve your issue? – Timofey Solonin Nov 06 '17 at 19:06
  • No After making the change also I still have the same error – A.S Nov 06 '17 at 19:30
  • Could you please provide a bit more information about the error you are getting? Maybe you are binding an error to UI or working with UI side effects on an incorrect thread? – Timofey Solonin Nov 06 '17 at 20:54
  • I have edited the question. When ever the observer returns an error it crashes. Looks like I am trying to bind an error to the UI. Any help on how I should be handling this? – A.S Nov 07 '17 at 04:14

1 Answers1

0

If provider.request(.signIn(userData!)) // ... returns results on some background thread, results would be bound to UI elements from a background thread which could cause non-deterministic crashes.

It should be

provider.request(.signIn(userData!))
            .observeOn(MainScheduler.instance) // ...

according to RxSwift github tips: Drive

dengApro
  • 3,848
  • 2
  • 27
  • 41