rxswift Chaining:在rxswift中编写此内容的更好方式



我是RxSwift的新手。非常新。通常,要在没有RX的情况下链接您,您将下一个功能放入完成闭合中。我的同行告诉我使用Flatmap或我们的ConcatMap版本,但从未给我一个例子。有人可以使用Flatmap或Swift版本的ConcatMap转换此代码吗?再次,我很新,所以请对判断保持温和。

 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)

所以,您想要的永远不会嵌套subscribe调用。相反,您会将可观察值的值,有时转换为另一个值,而其他可观察到的值。

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)

因此,在这里,第一个postSeekerAvatar将被订阅,每次推出一个值时,updateSeeker也将被订阅。如果两个错误出现,则错误关闭。

postSeekerAvatar() method should return a Single response 
Add showLoader/hideLoader method in BaseViewController. 
viewmodel
    .postSeekerAvatar(image: image) //Return Single<Response>
    .do(onSubscribed: presenter.showLoader)
    .do(onDispose: presenter.hideLoader)
    .asCompletable()
    .andThen(update(params: params))
    .subscribe()
    .disposeOnDeactivate(interactor: self)
//Returns a completable 
  private func update(params: Model) -> Completable {
    viewmodel
      .updateSeeker(
        withFirstName: params.firstName,
        lastName: params.lastName,
        isBusiness: params.isBusiness,
        abn: "none",
        address: params.address
    )
    return Completable.empty()
  }

最新更新