如何在 Rx Observable 中包装 SKPaymentQueue
How to wrap SKPaymentQueue in Rx Observable
有谁知道您将如何包装 SKPaymentQueue in an Rx Observable? SKPaymentQueue requires that you register an observer that conforms to the SKPaymentTransactionObserver via the SKPaymentQueue.addTransactionObserver 方法。
到目前为止,我已经尝试了以下方法,但它不起作用。我怀疑是因为观察者代理在被调用之前被释放了。感觉我在这里缺少某种惯用的 Rx 技巧:
class StoreService {
let paymentQueue = SKPaymentQueue.defaultQueue()
func purchase(product: SKProduct) -> Observable<SKProduct> {
return Observable.create { (observer) in
let transactionObserver = PaymentTransactionProxyObserver(observer)
self.paymentQueue.addTransactionObserver(transactionObserver)
let payment = SKPayment(product: product)
self.paymentQueue.addPayment(payment)
return AnonymousDisposable {
self.paymentQueue.removeTransactionObserver(transactionObserver)
}
}
}
}
class PaymentTransactionProxyObserver: NSObject, SKPaymentTransactionObserver {
let observer: AnyObserver<SKPaymentTransaction>
init(_ observer: AnyObserver<SKPaymentTransaction>) {
self.observer = observer
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
observer.onNext(transaction)
}
observer.onCompleted()
}
}
我也在 RxCocoa 源代码中四处寻找灵感,但是最接近的示例 NSNotificationCenter 扩展使用闭包而不是具有多个回调方法的观察者注册和注销步骤。它似乎也不适合其他扩展使用的 DelegateProxy 模式。
你说得对,你的代理被删除得太快了。 PaymentTransactionProxyObserver
需要持有对自身的引用以保持自身存活,然后在调用 onCompleted
.
后取消该引用
这里有一个灵感要点。它使用 Promises 而不是 Rx,但它应该可以帮助您理解。查看此代码时,将 PromiseKit 的 fulfill
方法视为 observer.onNext; observer.onCompleted
,将 PromiseKit 的 reject
视为 observer.onError
.
https://gist.github.com/dtartaglia/b92163d6055e1a580d493676e5b1e448
有谁知道您将如何包装 SKPaymentQueue in an Rx Observable? SKPaymentQueue requires that you register an observer that conforms to the SKPaymentTransactionObserver via the SKPaymentQueue.addTransactionObserver 方法。
到目前为止,我已经尝试了以下方法,但它不起作用。我怀疑是因为观察者代理在被调用之前被释放了。感觉我在这里缺少某种惯用的 Rx 技巧:
class StoreService {
let paymentQueue = SKPaymentQueue.defaultQueue()
func purchase(product: SKProduct) -> Observable<SKProduct> {
return Observable.create { (observer) in
let transactionObserver = PaymentTransactionProxyObserver(observer)
self.paymentQueue.addTransactionObserver(transactionObserver)
let payment = SKPayment(product: product)
self.paymentQueue.addPayment(payment)
return AnonymousDisposable {
self.paymentQueue.removeTransactionObserver(transactionObserver)
}
}
}
}
class PaymentTransactionProxyObserver: NSObject, SKPaymentTransactionObserver {
let observer: AnyObserver<SKPaymentTransaction>
init(_ observer: AnyObserver<SKPaymentTransaction>) {
self.observer = observer
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
observer.onNext(transaction)
}
observer.onCompleted()
}
}
我也在 RxCocoa 源代码中四处寻找灵感,但是最接近的示例 NSNotificationCenter 扩展使用闭包而不是具有多个回调方法的观察者注册和注销步骤。它似乎也不适合其他扩展使用的 DelegateProxy 模式。
你说得对,你的代理被删除得太快了。 PaymentTransactionProxyObserver
需要持有对自身的引用以保持自身存活,然后在调用 onCompleted
.
这里有一个灵感要点。它使用 Promises 而不是 Rx,但它应该可以帮助您理解。查看此代码时,将 PromiseKit 的 fulfill
方法视为 observer.onNext; observer.onCompleted
,将 PromiseKit 的 reject
视为 observer.onError
.
https://gist.github.com/dtartaglia/b92163d6055e1a580d493676e5b1e448