From c8f4363b37063de088f0a3c4910c887ea3acc394 Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Mon, 31 Jul 2023 12:01:55 +0200 Subject: [PATCH] `Paywalls`: pre-warm intro eligibility in background thread (#2925) Follow-up to #2860. Thanks to integration tests for catching this (#2123). ![image](https://github.com/RevenueCat/purchases-ios/assets/685609/cf1e8e32-5d5e-47ba-b900-394aa780d865) --- Sources/Purchasing/Purchases/Purchases.swift | 27 +++++++++++-------- Sources/Support/FrameworkDisambiguation.swift | 1 + 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Sources/Purchasing/Purchases/Purchases.swift b/Sources/Purchasing/Purchases/Purchases.swift index 71e6f24719..691cc8c8d5 100644 --- a/Sources/Purchasing/Purchases/Purchases.swift +++ b/Sources/Purchasing/Purchases/Purchases.swift @@ -1615,21 +1615,26 @@ private extension Purchases { self.offeringsManager.updateOfferingsCache(appUserID: self.appUserID, isAppBackgrounded: isAppBackgrounded) { offerings in if let offering = offerings.value?.current, let paywall = offering.paywall { - let packageTypes = Set(paywall.config.packages) - let products: Set = .init( - offering.availablePackages - .lazy - .filter { packageTypes.contains($0.packageType) } - .map(\.storeProduct.productIdentifier) - ) - - Logger.debug(Strings.eligibility.warming_up_eligibility_cache(paywall)) - - self.trialOrIntroPriceEligibilityChecker.checkEligibility(productIdentifiers: products) { _ in } + self.operationDispatcher.dispatchOnWorkerThread { + self.warmUpEligibilityCache(offering: offering, paywall: paywall) + } } } } + private func warmUpEligibilityCache(offering: RCOffering, paywall: PaywallData) { + let packageTypes = Set(paywall.config.packages) + let products: Set = .init( + offering.availablePackages + .lazy + .filter { packageTypes.contains($0.packageType) } + .map(\.storeProduct.productIdentifier) + ) + + Logger.debug(Strings.eligibility.warming_up_eligibility_cache(paywall)) + self.trialOrIntroPriceEligibilityChecker.checkEligibility(productIdentifiers: products) { _ in } + } + } // MARK: - Deprecations diff --git a/Sources/Support/FrameworkDisambiguation.swift b/Sources/Support/FrameworkDisambiguation.swift index 8a36af032a..e6e848f309 100644 --- a/Sources/Support/FrameworkDisambiguation.swift +++ b/Sources/Support/FrameworkDisambiguation.swift @@ -23,6 +23,7 @@ typealias RCRefundRequestStatus = RefundRequestStatus typealias RCErrorCode = ErrorCode +typealias RCOffering = Offering typealias RCStorefront = Storefront let RCDefaultLogHandler = defaultLogHandler