用决策树观察

时间:2016-10-13 20:32:43

标签: swift reactjs rx-java rx-swift

我需要一个函数将一个复杂的IAP购买树封装到一个简单的attemptPurchase函数中,该函数返回一个boolean observable(true - > success,false - > cancelled,error - > any error)

但我对如何创建该功能感到困惑,主要是因为决定的开始是异步的。

以下决策树和代码=>非常感谢!

enter image description here

// fails -> missing return function
// but i cannot return the credit check, since the execution is different depending on the result

func attemptPurchase(amount: Int) -> Observable<Bool>{

  let creditCheck = creditCheck(amount)

  creditCheck.filter{$0}.subscribeNext{ _ in
    return Observable.just(true)
  }

  creditCheck.filter{$0}.subscribeNext{ _ in
    return confirmIAP().processIAP()
  }
}

func creditCheck(amount: Int) -> Observable<Bool>{
  return API.creditCheck.map{$0 > amount}
}

func confirmIAP() -> Observable<Bool> {
  // UI for confirming IAP
}

func processIAP() -> Observable<Bool> {
  // UI for uploading IAP on my server
}

2 个答案:

答案 0 :(得分:1)

你可以这样做:

break

答案 1 :(得分:0)

来自iankeen在RxSwift松弛组中的回答:

func attemptPurchase(amount: Int) -> Observable<Bool>{
    return creditCheck(amount)
        .flatMap { enough in
            return (enough ? .just(true) : confirmIAP())
        }
        .flatMap { ready in
            guard ready else { /* failure */ }
            return processIAP()
        }
}