I'm very new to RxSwift. Very new. Normally to chain you without Rx, you put the next function inside a completion closure. My peers told me to use flatmap or our version of concatmap instead but never gave me an example. Can anybody convert this code with flatmap or the swift version of concatmap? Again I am very new, so please be gentle with the judgements.
SVProgressHUD.show()
params.address = address
print(params)
viewmodel
.postSeekerAvatar(image: image).subscribe(onSuccess: {
// SVProgressHUD.dismiss()
// SVProgressHUD.show()
self.viewmodel
.updateSeeker(withFirstName: params.firstName, lastName: params.lastName, isBusiness: params.isBusiness, abn: "none", address: params.address)
.subscribe(onSuccess: {
SVProgressHUD.dismiss()
self.performSegue(withIdentifier: SegueConstants.toProfileForm, sender: self)
}) { (error) in
SVProgressHUD.showDismissableError(with: error.message)
}.disposed(by: self.disposeBag)
}) { (error) in
SVProgressHUD.showDismissableInfo(with: error.message)
}.disposed(by: disposeBag)
So, what you want is never have nested subscribe
calls. Instead, you will transform the source observable values, sometimes into another value, other times with another observable.
SVProgressHUD.show()
params.address = address
print(params)
viewmodel
.postSeekerAvatar(image: image)
.flatMap {
self.viewmodel
.updateSeeker(withFirstName: params.firstName, lastName: params.lastName, isBusiness: params.isBusiness, abn: "none", address: params.address)
}
.subscribe(onSuccess: {
SVProgressHUD.dismiss()
self.performSegue(withIdentifier: SegueConstants.toProfileForm, sender: self)
}, onError: { (error) in
SVProgressHUD.showDismissableError(with: error.message)
})
.disposed(by: disposeBag)
So here, first postSeekerAvatar
will be subscribed to, and everytime it pushes out a value, updateSeeker
will also be subscribed to. If either of them errors out, the error closure will run.