From e78c56d11fcbc9893904545f015d4205bbb157da Mon Sep 17 00:00:00 2001 From: Will Taylor Date: Mon, 16 Dec 2024 08:08:22 -0600 Subject: [PATCH] Always call readyForPromotedProduct on the main actor --- Sources/Purchasing/Purchases/Purchases.swift | 8 ++++++- .../PurchasesDeferredPurchasesTests.swift | 21 ++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Sources/Purchasing/Purchases/Purchases.swift b/Sources/Purchasing/Purchases/Purchases.swift index 2996ad19d3..156fcba9d5 100644 --- a/Sources/Purchasing/Purchases/Purchases.swift +++ b/Sources/Purchasing/Purchases/Purchases.swift @@ -1597,7 +1597,13 @@ extension Purchases: PurchasesOrchestratorDelegate { */ func readyForPromotedProduct(_ product: StoreProduct, purchase startPurchase: @escaping StartPurchaseBlock) { - self.delegate?.purchases?(self, readyForPromotedProduct: product, purchase: startPurchase) + OperationDispatcher.dispatchOnMainActor { + self.delegate?.purchases?( + self, + readyForPromotedProduct: product, + purchase: startPurchase + ) + } } #if os(iOS) || targetEnvironment(macCatalyst) || VISION_OS diff --git a/Tests/UnitTests/Purchasing/Purchases/PurchasesDeferredPurchasesTests.swift b/Tests/UnitTests/Purchasing/Purchases/PurchasesDeferredPurchasesTests.swift index ff2fc73790..5ca53ee962 100644 --- a/Tests/UnitTests/Purchasing/Purchases/PurchasesDeferredPurchasesTests.swift +++ b/Tests/UnitTests/Purchasing/Purchases/PurchasesDeferredPurchasesTests.swift @@ -42,6 +42,12 @@ class PurchaseDeferredPurchasesTests: BasePurchasesTests { shouldAddStorePayment: payment, for: self.product) + waitUntil { completed in + if self.purchasesDelegate.makeDeferredPurchase != nil { + completed() + } + } + expect(self.purchasesDelegate.makeDeferredPurchase).toNot(beNil()) expect(self.storeKit1Wrapper.payment).to(beNil()) @@ -59,6 +65,12 @@ class PurchaseDeferredPurchasesTests: BasePurchasesTests { shouldAddStorePayment: payment, for: self.product) + waitUntil { completed in + if self.purchasesDelegate.makeDeferredPurchase != nil { + completed() + } + } + expect(self.purchasesDelegate.makeDeferredPurchase).toNot(beNil()) expect(self.storeKit1Wrapper.payment).to(beNil()) @@ -87,7 +99,7 @@ class PurchaseDeferredPurchasesTests: BasePurchasesTests { shouldAddStorePayment: payment, for: self.product) - expect(self.purchasesDelegate.promoProduct) == StoreProduct(sk1Product: self.product) + expect(self.purchasesDelegate.promoProduct).toEventually(equal(StoreProduct(sk1Product: self.product))) } func testShouldAddStorePaymentReturnsFalseForNilProductIdentifier() throws { @@ -177,10 +189,9 @@ class PurchaseDeferredPurchasesSK2Tests: BasePurchasesTests { for: self.product ) - expect(self.purchasesDelegate.makeDeferredPurchase).toNot(beNil()) - - expect(self.purchasesDelegate.promoProduct) == StoreProduct(sk1Product: self.product) - expect(self.purchasesDelegate.makeDeferredPurchase).toNot(beNil()) + expect(self.purchasesDelegate.makeDeferredPurchase).toEventuallyNot(beNil()) + expect(self.purchasesDelegate.promoProduct).toEventually(equal(StoreProduct(sk1Product: self.product))) + expect(self.purchasesDelegate.makeDeferredPurchase).toEventuallyNot(beNil()) } }