I'm a very beginner with RxSwift and I'm trying to begin with a simple login screen. So I have 2 text fields and a login button, which is bind to a PublishSubject
so every time I tap the button, I'll send a network request to perform authentication.
Since authentication may fails, I went with a Driver
so I could replay my request each time I click the button.
I have 2 version of what I think is the same code but one works and one doesn't. I'm trying to understand what happens behind the scene.
Here the first version which works (request every time I touch the button) :
let credentials = Driver.combineLatest(email.asDriver(), password.asDriver()) { ($0, $1) }
self.signIn = signInTaps
.asDriver(onErrorJustReturn: ())
.withLatestFrom(credentials)
.flatMapLatest { email, password in // returns Driver<Result<AuthenticateResponse, APIError>>
return provider.request(.Authenticate(email: email, password: password))
.filterSuccessfulStatusCodes()
.mapObject(AuthenticateResponse)
.map { element -> Result<AuthenticateResponse, APIError> in
return .Success(element)
}
.asDriver { error in
let e = APIError.fromError(error)
return Driver<Result<AuthenticateResponse, APIError>>.just(.Failure(e))
}
.debug()
}
And here's the one which doesn't work (request fires only on first click) :
let credentials = Observable.combineLatest(email.asObservable(), password.asObservable()) { ($0, $1) }
self.signIn = signInTaps.asObservable()
.withLatestFrom(c)
.flatMapLatest { email, password in // returns Observable<AuthenticateResponse>
return provider.request(.Authenticate(email: email, password: password))
.filterSuccessfulStatusCodes()
.mapObject(AuthenticateResponse)
}
.map { element -> Result<AuthenticateResponse, APIError> in // returns Observable<Result<AuthenticateResponse, APIError>>
return .Success(element)
}
.asDriver { error in // returns Driver<Result<AuthenticateResponse, APIError>>
let e = APIError.fromError(error)
return Driver<Result<AuthenticateResponse, APIError>>.just(.Failure(e))
}
.debug()
For information, here's my properties declaration :
let email = Variable("")
let password = Variable("")
let signInTaps = PublishSubject<Void>()
let signIn: Driver<Result<AuthenticateResponse, APIError>>