RxSwift Chaining:在Rxswift中编写这个的更好的方法是什么?

问题描述 投票:0回答:2

我对RxSwift很新。很新。通常在没有Rx的情况下链接你,你将下一个函数放在完成闭包中。我的同行告诉我使用flatmap或我们的concatmap版本,但从未给我一个例子。任何人都可以使用flatmap或swatmap的swift版本转换此代码吗?我再次非常新,所以请对判断保持温和。

 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)
swift rx-swift
2个回答
1
投票

所以,你想要的是从来没有嵌套的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也将被订阅。如果其中任何一个错误,将运行错误关闭。


1
投票
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()
  }
© www.soinside.com 2019 - 2024. All rights reserved.