RxSwift Chaining requests - swift

The problem I faced with is chaining 2 requests and handling errors.
Below an example of my code:
func fbLogin() -> Observable<String> { ... }
func userLogin(request: Request) -> Observable<User> { ... }
let signedWithLogin = loginTaps
.asDriver(onErrorJustReturn: ())
.flatMapLatest { _ in
return fbLogin()
.map({ ReqestState<String>.loaded($0) })
.asDriver(onErrorRecover: { error in
return Driver.just(.error(error))
})
.startWith(.loading)
}
.map({ UserEndpoint.socialLogin(token: $0) })
.flatMapLatest { request in
return userLogin(request: request)
.map({ ReqestState<User>.loaded($0) })
.asDriver(onErrorRecover: { error in
return Driver.just(.error(error))
})
.startWith(.loading)
}
signedWithLogin
.drive(onNext: { response in
print(response)
})
.disposed(by: disposeBag)
Problem is when I cancel the facebook login popup I send observer.onError(FBLoginManagerError.canceled) error. This error catch first .asDriver(onErrorRecover: { error method but does't pass to the .drive(onNext: { responsemethod.
How can I catch all errors in .asDriver(onErrorRecover: { error method?

Mukesh is correct that you probably should avoid Driver until the end. Also, there's little point of having both RequestState types when you only really care about the final one (RequestState<User>)
Here's a simpler version that I think will do what you want:
let signedWithLogin = loginTaps
.flatMapLatest {
fbLogin()
.map { UserEndpoint.socialLogin(token: $0) }
.flatMap { userLogin(request: $0) }
.map { RequestState.loaded($0) }
.catchError { .just(.error($0)) }
.startWith(.loading)
}
.asDriver(onErrorRecover: { fatalError($0.localizedDescription) }) // I'm using `fatalError()` here because if the above emits an error something has gone horribly wrong (like the RxSwift library isn't working anymore.)
signedWithLogin
.drive(onNext: { response in
print(response)
})
.disposed(by: disposeBag)
The above assumes you change your UserEndpoint.socialLogin(token:) function to accept a String instead of a RequestState<String>.
It also assumes that fbLogin() and userLogin(request:) only emit one value each. You might want to consider switching them to Singles.

Related

Run multiple request at a time and continue as soon there is success

Given an array of urls, is there a way to run those at once simultaneously? But in such a way so as soon as there is one success move to the next request using that successful url.
So far I tried chaining concatMap, and failed with zip.
func updateAccountInfo() -> Single<Bool> {
var disposable: Disposable? = nil
return Single<Bool>.create { observer in
do {
var urls = try self.settings.domains.value()
disposable = Observable.from(urls)
.enumerated()
.concatMap { index, url -> Single<URL> in
return self.verifyUrl(url)
}
.concatMap { url -> Single<Account> in
return self.apiManager.loadAccountInfo(from: url)
}
.observeOn(MainScheduler.instance)
.do(onNext: { (account: AccountInfo) in
// use account unfo here
disposable!.dispose()
})
.subscribe()
} catch {
observer(.error(error))
}
return Disposables.create()
}
}
Tried like so too:
disposable = Observable.from(urls)
.enumerated()
.concatMap { index, url -> Single<(Bool, URL)> in
return self.verifyUrl(url)
}
.subscribe(onNext: { reachable, url in
if reachable {
self.apiManager.loadAccountInfo(from: url)
.subscribe(onSuccess: { accountInfo in
// use account info here
}, onError: { error in
})
.disposed(by: self.bag)
disposable!.dispose()
} else {
}
}, onError: { error in
}, onCompleted: {
})
Maybe I use zip but how would I create an array of verifyUrl(url) calls? Does zip accept arrays of Observable at all?
let obs = Observable.from(urls)
.enumerated()
.concatMap { index, url -> Single<URL> in
return self.verifyUrl(url)
}
let test = Observable
.zip(obs).map { [urls] in
return [urls]
}
If I understand the question correctly, you are looking for something like this:
func example() throws {
let urls = try self.settings.domains.value()
Observable.merge(urls.map { verifyUrl($0).asObservable() })
.flatMap { [apiManager] url in
apiManager!.loadAccountInfo(from: url)
}
.observe(on: MainScheduler.instance)
.subscribe(onNext: { account in
// use account unfo here
})
.disposed(by: self.disposeBag)
}
But it's hard to tell. Your code samples are a bit jumbled. Putting all your code in a Single.create is odd. Returning a Disposables.create() from the closure when you have a disposable to return is odd. Calling dispose() on a disposable inside the do block is odd. So much weirdness... I suggest you post some code on https://codereview.stackexchange.com or look at sample code.

call func in Asynchronous requests chaining RxSwift and Alamofire

I'm a begginer with RxSwift and this is my issue, my app must do a 3 requests, the 3 are gets, my work team suggests me use a flatmap to do this a three request in row, but I don't know how I should use flatmap.
these are my requests
public func login(param: [String:String]) -> Observable<messageModel>{
return Observable.create { observer -> Disposable in
self.alamoFireManager!.request(self.urlServer!+endPoints.login.login, method: .post, parameters: param, encoding: URLEncoding.default, headers: nil, interceptor: nil).responseDecodable { (res: DataResponse<messageModel,AFError>) in
if let error = res.error {
observer.onError(error)
} else if let valueEntitie = res.value {
observer.onNext(valueEntitie)
}
observer.onCompleted()
}
return Disposables.create()
}
}
public func me() -> Observable<meModel>{
return Observable.create { observer -> Disposable in
self.alamoFireManager!.request(self.urlServer!+endPoints.login.me, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil, interceptor: nil).responseDecodable { (res: DataResponse<meModel,AFError>) in
if let error = res.error {
observer.onError(error)
} else if let valueEntitie = res.value {
observer.onNext(valueEntitie)
}
observer.onCompleted()
}
return Disposables.create()
}
}
public func entitie(entityId: String) -> Observable<entitieModel>{
return Observable.create { observer -> Disposable in
self.alamoFireManager!.request(self.urlServer!+endPoints.login.entities+"/"+entityId, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil, interceptor: nil).responseDecodable { (res: DataResponse<entitieModel,AFError>) in
if let error = res.error {
observer.onError(error)
} else if let valueEntitie = res.value {
observer.onNext(valueEntitie)
}
observer.onCompleted()
}
return Disposables.create()
}
}
the first endpoint is login with its parameters, then me, function me response a id, this id its necessary for the third request, that is entitie.
I'm doing the flatmap as this way.
networkManagerShareCore.share.login(param: param)
.flatMap { resMessageModel in
//saveData(resMessageModel)
networkManagerShareCore.share.me()
.flatMap { resMeModel in
//saveData(resMessageModel)
networkManagerShareCore.share.entitie(entityId: "\(resModelMe.data.personId!)")
}
}.subscribe(onNext: { (model) in
print(model)
}, onError: { (error) in
self.errorMsg.accept(error.localizedDescription)
self.isSuccess.accept(false)
}, onCompleted: nil) {
print("Disposed")
}
the code works, but in the line //saveData(resMessageModel) I can't call it the function, I want to save the model, but if I try a call a function, Xcode show me this error: Unable to infer complex closure return type; add explicit type to disambiguate
so, how can I resolve this?
RxSwift flatMap expects a return value of some Observable.
The FlatMap operator transforms an Observable by applying a function
that you specify to each item emitted by the source Observable, where
that function returns an Observable that itself emits items.
You need to add a return statement within each flatMap call. In addition, you need to explicitly define the return type for the closures.
So the inner most flatMap call should look something like this:
networkManagerShareCore.share.me()
.flatMap { resMeModel -> Observable<entitieModel> in
//saveData(resMessageModel)
return networkManagerShareCore.share.entitie(entityId: "\(resModelMe.data.personId!)")
You wouldn't normally embed flatMap within flatMap, but just compose them sequentially to make the code clearer. And don't forget disposed(by:). The final result would look something like this:
networkManagerShareCore.share.login(param: param)
.flatMap { resMessageModel -> Observable<meModel> in
//saveData(resMessageModel)
return networkManagerShareCore.share.me()
}
.flatMap { resMeModel -> Observable<entitieModel> in
//saveData(resMessageModel)
return networkManagerShareCore.share.entitie(entityId: "\(resModelMe.data.personId!)")
}
.subscribe(
onNext: { (model) in
print(model)
},
onError: { (error) in
self.errorMsg.accept(error.localizedDescription)
self.isSuccess.accept(false)
},
onCompleted: nil) {
print("Disposed")
}
.disposed(by: disposeBag)
Dale's answer works but I would be inclined to move the saves into do operators instead. Like this:
networkManagerShareCore.share.login(param: param)
.do(onNext: { [weak self] resMessageModel in
self?.saveData(resMessageModel)
})
.flatMap { _ in
networkManagerShareCore.share.me()
}
.do(onNext: { [weak self] resMeModel in
self?.saveData(resMeModel)
})
.flatMap { resMeModel in
networkManagerShareCore.share.entitie(entityId: "\(resMeModel.data.personId!)")
}
.subscribe(
onNext: { (model) in
print(model)
},
onError: { (error) in
self.errorMsg.accept(error.localizedDescription)
self.isSuccess.accept(false)
},
onCompleted: nil,
onDisposed: {
print("Disposed")
}
)
.disposed(by: disposeBag)

RxSwift+Alamofire custom mapper error handling

RxSwift one more question about error handling:
I'm using Alamofire+RxAlamofire this way:
SessionManager.default.rx.responseJSON(.post, url, parameters:params)
example:
func login() -> Observable<Int> {
let urlString = ...
let params = ...
return SessionManager.default.rx.responseJSON(.post, url, parameters:params)
.rxJsonDefaultResponse()
.map({ (data) in
data["clientId"] as! Int
})
}
....
extension ObservableType where Element == (HTTPURLResponse, Any) {
func rxJsonDefaultResponse() -> Observable<Dictionary<String, Any>> {
return self.asObservable().map { data -> Dictionary<String, Any> in
if... //error chechings
throw NSError(domain: ..,
code: ...,
userInfo: ...)
}
...
return json
}
}
}
using:
loginBtn.rx.tap
.flatMap{ _ in
provider.login()
}.subscribe(onNext: { id in
...
}, onError: { (er) in
ErrorPresentationHelper.showErrorAlert(for: er)
})
.disposed(by: bag)
So if error occurred everything works as intended: error alert shows and 'loginBtn.rx.tap' disposed, but I need it to be still alive, what's my strategy here if I want to use onError block?
You can use materialize function in rxSwift. It will convert any Observable into an Observable of its events. So that you will be listening to Observable<Event<Int>> than Observable<Int>. Any error thrown from the flatmap would be captured as error event in your subscription block's onNext and can be handled there. And your subscription would still be alive. Sample code would be as follows.
button.rx.tap.flatMap { _ in
return Observable.just(0)
.flatMap { _ -> Observable<Int> in
provider.login()
}.materialize()
}.subscribe(onNext: { event in
switch event {
case .next:
if let value = event.element {
print(value) //You will be getting your value here
}
case .error:
if let error = event.error {
print(error.localizedDescription) //You will be getting your captured error here
}
case .completed:
print("Subscription completed")
}
}) {
print("Subscription disposed")
}.disposed(by: disposeBag)
Hope it helps. You can checkout the materialize extension here.

How do you sequentially chain observables in concise and readable way

Im new to RXSwift and I've begun investigating how I can perform Promise like function chaining.
I think I'm on the right track by using flatmap but my implementation is very difficult to read so I suspect theres a better way to accomplish it.
What I have here seems to work but I'm getting a headache thinking about what It might looks like if I added another 3 or functions to the chain.
Here Is where I declare my 'promise chain'(hard to read)
LOGIN().flatMap{ (stuff) -> Observable<Int> in
return API(webSiteData: stuff).flatMap
{ (username) -> Observable<ProfileResult> in
return accessProfile(userDisplayName: username) }
}.subscribe(onNext: { event in
print("The Chain Completed")
print(event)
}, onError:{ error in
print("An error in the chain occurred")
})
These are the 3 sample functions I'm chaining
struct apicreds
{
let websocket:String
let token:String
}
typealias APIResult = String
typealias ProfileResult = Int
// FUNCTION 1
func LOGIN() -> Observable<apicreds> {
return Observable.create { observer in
print("IN LOGIn")
observer.onNext(apicreds(websocket: "the web socket", token: "the token"))
observer.on(.completed)
return Disposables.create()
}
}
// FUNCTION 2
func API(webSiteData: apicreds) -> Observable<APIResult> {
return Observable.create { observer in
print("IN API")
print (webSiteData)
// observer.onError(myerror.anError)
observer.on(.next("This is the user name")) // assiging "1" just as an example, you may ignore
observer.on(.completed)
return Disposables.create()
}
}
//FUNCTION 3
func accessProfile(userDisplayName:String) -> Observable<ProfileResult>
{
return Observable.create { observer in
// Place your second server access code
print("IN Profile")
print (userDisplayName)
observer.on(.next(200)) // 200 response from profile call
observer.on(.completed)
return Disposables.create()
}
}
This is a very common problem we run into while chaining operations. As a beginner I had written similar code using RxSwift in my projects as well. And there are two areas of improvement -
1. Refactor the code to remove nested flatMaps
2. Format it differently to make the sequence easier to follow
LOGIN()
.flatMap{ (stuff) -> Observable<APIResult> in
return API(webSiteData: stuff)
}.flatMap{ (username) -> Observable<ProfileResult> in
return accessProfile(userDisplayName: username)
}.subscribe(onNext: { event in
print("The Chain Completed")
print(event)
}, onError:{ error in
print("An error in the chain occurred")
})
In addition to nested flatMap and code formatting, you could omit return and explicit return types:
LOGIN()
.flatMap { webSiteData in API(webSiteData: webSiteData) }
parameter names
LOGIN()
.flatMap { API(webSiteData: $0) }
or even remove parameters at all where appropriate:
LOGIN()
.flatMap(API)
.flatMap(accessProfile)
.subscribe(
onNext: { event in
print(event)
}, onError:{ error in
print(error)
}
)
FYI there is Observable.just method which would be convenient here:
struct ApiCredentials {
let websocket: String
let token: String
}
func observeCredentials() -> Observable<ApiCredentials> {
let credentials = ApiCredentials(websocket: "the web socket", token: "the token")
return Observable.just(credentials)
}
Try to follow official Swift API Guidelines to make your code more readable.
You can also use the point-free style and just pass function references to flatMap:
LOGIN()
.flatMap(API)
.flatMap(accessProfile)
.subscribe(onNext: { event in
print("The Chain Completed")
print(event)
}, onError:{ error in
print("An error in the chain occurred")
})

RxSwift Chaining two signals in right order

So basically I have two actions I need to execute:
first is login
second is get user profile
They have to be done in right order because getting user profile cannot be done without logging in first.
So I had bunch of code that looked like this:
func signIn(signinParameters: SignInParameters) -> Observable<SignInResult> {
return Observable<SignInResult>.create { [unowned self] observer in
self.signinParameters = signinParameters
self.apiConnector
.signIn(with: signinParameters)
.do(onNext: { [weak self] signinResult in
self!.apiConnector
.get()
.do(onNext: { user in
let realm = RealmManager.shared.newRealm()!
let realmUser = RealmUser()
realmUser.configure(with: user, in: realm)
try? realm.write {
realm.add(realmUser, update: true)
}
self!.setState(.authenticated)
observer.onNext(signinResult)
}, onError: { (error) in
observer.onError(error)
}, onCompleted: {
observer.onCompleted()
}).subscribe()
}, onError: { error in
observer.onError(error)
}, onCompleted: {
print("completed")
observer.onCompleted()
}).subscribe()
return Disposables.create()
}
I know this is not right because I cannot send onNext signal with signin result when both actions are finished. I've been reading and I figured out i need to flatmap both actions, combine them into one signal and then manipulate signinresult but I dont have a clue how to do that. So any help would be nice.
Thank you
EDIT 1:
so I've refactored code to look something like this, but there is still problem that I can't send signal when BOTH actions are finished, or am I wrong?
func signIn(signinParameters: SignInParameters) -> Observable<SignInResult> {
return Observable<SignInResult>.create { [unowned self] observer in
self.signinParameters = signinParameters
self.apiConnector
.signIn(with: signinParameters)
.do(onNext: { (result) in
}, onError: { (error) in
}, onCompleted: {
})
.flatMap({ (result) -> Observable<User> in
self.apiConnector.get().asObservable()
})
.do(onNext: { (user) in
}, onError: { (error) in
}, onCompleted: {
}).subscribe()
return Disposables.create()
}
}
Your code is not very clean and it is hard to understand what is going on (my opinion).
If you need two actions to be executed you can create two functions:
struct Parameters{}
struct Profile{}
struct User{}
func login(parameters: Parameters) -> Observable<User> {
// get user
}
func profile(user: User) -> Observable<Profile> {
// get profile
}
func serial(parameters: Parameters) -> Observable<Profile> {
return login(parameters: parameters).flatMap({ profile(user: $0) })
}
login function or profile function can be also split into smaller functions if required:
func profileStored(user: User) -> Observable<Profile?> {
// get stored profile
}
func profileRequested(user: User) -> Observable<Profile> {
// get profile from network
}
func profile(user: User) -> Observable<Profile> {
let observable = profileStored(user: user)
.shareReplayLatestWhileConnected()
let observableStored = observable
.filter({ $0 != nil })
.map({ $0! })
.shareReplayLatestWhileConnected()
let observableRequested = observable
.filter({ $0 == nil })
.flatMap({ _ in profileRequested(user: user) })
.shareReplayLatestWhileConnected()
return Observable
.of(observableStored, observableRequested)
.merge()
.shareReplayLatestWhileConnected()
}
As a result you can mix smaller functions with flatMap or any other operator.
That is how I do it. Hope it'll be helpful