call func in Asynchronous requests chaining RxSwift and Alamofire - swift

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)

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.

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.

RxSwift Chaining requests

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.

RxSwift Chaining requests that depend from each other

I'm still learning Rx and I'm trying to use RxSwift to make 3 requests that return depend on each other.
* DocumentsA
- CollectionA
* DocumentsB
- CollectionB
* DocumentsC
- CollectionC
The models are something like this:
struct DocumentA {
let documentsB: [DocumentB]
}
struct DocumentB {
let documentsC: [DocumentC]
}
struct DocumentsC {
let name: String
}
So using RxSwift, I'm trying to request each level of the document using separate methods for each document:
func fetchDocumentsA() -> Observable<DocumentA> {
return Observable.create { observer in
fetchDocumentsB().subscribe(onNext: { documentB in
if let documentA = DocumentA(documentB: documentB) {
observer.onNext(documentA)
}
}, onError: nil, onCompleted: {
observer.onCompleted()
}, onDisposed: nil).disposed(by: self.disposeBag)
return Disposables.create()
}
}
func fetchDocumentsB() -> Observable<DocumentB> {
return Observable.create { observer in
fetchDocumentsC().subscribe(onNext: { documentC in
if let documentB = DocumentB(documentC: documentC) {
observer.onNext(documentB)
}
}, onError: nil, onCompleted: {
observer.onCompleted()
}, onDisposed: nil).disposed(by: self.disposeBag)
return Disposables.create()
}
}
func fetchDocumentsC() -> Observable<DocumentC> {
return Observable.create { observer in
fetchName().subscribe(onNext: { name in
observer.onNext(DocumentC(name: name))
}, onError: nil, onCompleted: {
observer.onCompleted()
}, onDisposed: nil).disposed(by: self.disposeBag)
return Disposables.create()
}
}
Is there a better way to do this? It seems very convoluted.
In this case you can make this much nicer with map function, like in example bellow.
The Map operator applies a function of your choosing to each item emitted by the source Observable,
and returns an Observable that emits the results of these function
applications.
ReactiveX
func fetchDocumentsA() -> Observable<DocumentA> {
return fetchDocumentsB().map { (documentB) -> DocumentA in
DocumentA(documentB: documentB)
}
}
func fetchDocumentsB() -> Observable<DocumentB> {
return fetchDocumentsC().map { (documentC) -> DocumentB in
DocumentB(documentC: documentC)
}
}
func fetchDocumentsC() -> Observable<DocumentC> {
return fetchName().map { (name) -> DocumentC in
return DocumentC(name: name)
}
}

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