From ecaddfc541d7a2e7db9152af3151d6b344b85bdd Mon Sep 17 00:00:00 2001 From: Manabu Nakazawa Date: Mon, 14 Jun 2021 20:46:42 +1000 Subject: [PATCH 1/2] Fix "Nothing to Restore" even when transactions exist --- .../PaymentQueueController.swift | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/Sources/SwiftyStoreKit/PaymentQueueController.swift b/Sources/SwiftyStoreKit/PaymentQueueController.swift index 8747c9ac..29392e0a 100644 --- a/Sources/SwiftyStoreKit/PaymentQueueController.swift +++ b/Sources/SwiftyStoreKit/PaymentQueueController.swift @@ -106,7 +106,10 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver { private let completeTransactionsController: CompleteTransactionsController unowned let paymentQueue: PaymentQueue private var entitlementRevocation: EntitlementRevocation? - + private let restorationDispatchQueue = DispatchQueue( + label: "com.musevisions.SwiftyStoreKit.restorationDispatchQueue", + qos: DispatchQoS.background) + deinit { paymentQueue.remove(self) } @@ -236,19 +239,21 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver { * 3. complete transactions (transactionState: .purchased, .failed, .restored, .deferred) * Any transactions where state == .purchasing are ignored. */ - var unhandledTransactions = transactions.filter { $0.transactionState != .purchasing } - - if unhandledTransactions.count > 0 { - - unhandledTransactions = paymentsController.processTransactions(transactions, on: paymentQueue) - - unhandledTransactions = restorePurchasesController.processTransactions(unhandledTransactions, on: paymentQueue) - - unhandledTransactions = completeTransactionsController.processTransactions(unhandledTransactions, on: paymentQueue) + restorationDispatchQueue.sync { + var unhandledTransactions = transactions.filter { $0.transactionState != .purchasing } if unhandledTransactions.count > 0 { - let strings = unhandledTransactions.map { $0.debugDescription }.joined(separator: "\n") - print("unhandledTransactions:\n\(strings)") + + unhandledTransactions = paymentsController.processTransactions(transactions, on: paymentQueue) + + unhandledTransactions = restorePurchasesController.processTransactions(unhandledTransactions, on: paymentQueue) + + unhandledTransactions = completeTransactionsController.processTransactions(unhandledTransactions, on: paymentQueue) + + if unhandledTransactions.count > 0 { + let strings = unhandledTransactions.map { $0.debugDescription }.joined(separator: "\n") + print("unhandledTransactions:\n\(strings)") + } } } } @@ -262,11 +267,15 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver { } func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) { - restorePurchasesController.restoreCompletedTransactionsFailed(withError: error) + restorationDispatchQueue.async { [weak self] in + self?.restorePurchasesController.restoreCompletedTransactionsFailed(withError: error) + } } func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) { - restorePurchasesController.restoreCompletedTransactionsFinished() + restorationDispatchQueue.async { [weak self] in + self?.restorePurchasesController.restoreCompletedTransactionsFinished() + } } func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) { From e5bb4a53270e71f4f14831e0bf48929173a4c2b6 Mon Sep 17 00:00:00 2001 From: Manabu Nakazawa Date: Sun, 4 Jul 2021 19:51:58 +1000 Subject: [PATCH 2/2] Fix: sometimes the completion of restoring is queued earlier than updating transactions. --- Sources/SwiftyStoreKit/PaymentQueueController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftyStoreKit/PaymentQueueController.swift b/Sources/SwiftyStoreKit/PaymentQueueController.swift index 29392e0a..d27c3766 100644 --- a/Sources/SwiftyStoreKit/PaymentQueueController.swift +++ b/Sources/SwiftyStoreKit/PaymentQueueController.swift @@ -267,13 +267,13 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver { } func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) { - restorationDispatchQueue.async { [weak self] in + restorationDispatchQueue.asyncAfter(deadline: .now() + 0.0001) { [weak self] in self?.restorePurchasesController.restoreCompletedTransactionsFailed(withError: error) } } func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) { - restorationDispatchQueue.async { [weak self] in + restorationDispatchQueue.asyncAfter(deadline: .now() + 0.0001) { [weak self] in self?.restorePurchasesController.restoreCompletedTransactionsFinished() } }