Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PurchasedProductsFetcher: cache current entitlements #2507

Merged
merged 2 commits into from
May 20, 2023

Conversation

NachoSoto
Copy link
Contributor

To optimize potentially computing offline CustomerInfo, this caches what's likely the most expensive part.

@NachoSoto NachoSoto added the perf label May 18, 2023
@NachoSoto NachoSoto requested a review from a team May 18, 2023 21:12
self.cache = .init()

self.updatesObservation = Task<Void, Never>(priority: .utility) { [cache = self.cache] in
for await _ in StoreKit.Transaction.unfinished where cache.cachedInstance != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite working.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also making an integration test (I'm glad we have coverage for this there too):

@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *)
func testPurchasingTwoProductsWhileServerIsDown() async throws {
    let product1 = try await self.monthlyPackage.storeProduct
    let product2 = try await self.annualPackage.storeProduct

    self.serverDown()

    _ = try await Purchases.shared.purchase(product: product1)
    let info = try await Purchases.shared.purchase(product: product2).customerInfo

    try await self.verifyEntitlementWentThrough(info)
    expect(info.allPurchasedProductIdentifiers) == [
        product1.productIdentifier,
        product2.productIdentifier
    ]
}

private let appStoreSync: () async throws -> Void
private let sandboxDetector: SandboxEnvironmentDetector
private let cache: InMemoryCachedObject<Transactions>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you are planning to cache the transactions instead of the whole offline customer info? While that should probably solve the performance concerns it might not help to support fetching customer info from cache while in offline entitlements mode, unless we add support for recalculating it there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might not help to support fetching customer info from cache while in offline entitlements mode, unless we add support for recalculating it there.

The SDK already always returns customer info from the cache regardless of the mode. Is there a particular scenario you think wouldn't work?

@NachoSoto NachoSoto force-pushed the purchased-product-fetcher-cache branch from aca8339 to ffb1bcf Compare May 19, 2023 18:11
@NachoSoto NachoSoto force-pushed the purchased-product-fetcher-cache-2 branch 2 times, most recently from 191fa63 to 06e57d0 Compare May 19, 2023 18:14
Base automatically changed from purchased-product-fetcher-cache to main May 19, 2023 18:46
To optimize potentially computing offline `CustomerInfo`, this caches what's likely the most expensive part.
@NachoSoto NachoSoto force-pushed the purchased-product-fetcher-cache-2 branch from 06e57d0 to 1b61a82 Compare May 19, 2023 18:47
@codecov
Copy link

codecov bot commented May 19, 2023

Codecov Report

Attention: Patch coverage is 90.90909% with 6 lines in your changes missing coverage. Please review.

Project coverage is 87.81%. Comparing base (c0c6893) to head (1b61a82).
Report is 1130 commits behind head on main.

Files with missing lines Patch % Lines
...flineEntitlements/OfflineCustomerInfoCreator.swift 70.00% 3 Missing ⚠️
...OfflineEntitlements/PurchasedProductsFetcher.swift 92.00% 2 Missing ⚠️
...s/Logging/Strings/OfflineEntitlementsStrings.swift 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2507      +/-   ##
==========================================
- Coverage   87.82%   87.81%   -0.02%     
==========================================
  Files         198      198              
  Lines       13632    13685      +53     
==========================================
+ Hits        11972    12017      +45     
- Misses       1660     1668       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@NachoSoto NachoSoto merged commit 789a0e5 into main May 20, 2023
@NachoSoto NachoSoto deleted the purchased-product-fetcher-cache-2 branch May 20, 2023 01:08
This was referenced May 31, 2023
@NachoSoto NachoSoto mentioned this pull request May 31, 2023
NachoSoto added a commit that referenced this pull request Jun 1, 2023
**This is an automatic release.**

### New Features
* `Offline Entitlements`: use offline-computed `CustomerInfo` when
server is down (#2368) via NachoSoto (@NachoSoto)

### Bugfixes
* `AppleReceipt.debugDescription`: don't pretty-print JSON (#2564) via
NachoSoto (@NachoSoto)
* `SK2StoreProduct`: fix crash on iOS 12 (#2565) via NachoSoto
(@NachoSoto)
* `GetCustomerInfo` posts receipts if there are pending transactions
(#2533) via NachoSoto (@NachoSoto)
### Performance Improvements
* `PurchasedProductsFetcher`: cache current entitlements (#2507) via
NachoSoto (@NachoSoto)
* Performance: new check to ensure serialization / deserialization is
done from background thread (#2496) via NachoSoto (@NachoSoto)
### Dependency Updates
* Bump fastlane from 2.212.2 to 2.213.0 (#2544) via dependabot[bot]
(@dependabot[bot])
### Other Changes
* `CustomerInfoManager`: post all unfinished transactions (#2563) via
NachoSoto (@NachoSoto)
* `PostReceiptOperation`: added ability to also post `AdServices` token
(#2566) via NachoSoto (@NachoSoto)
* `Offline Entitlements`: improved computation log (#2562) via NachoSoto
(@NachoSoto)
* Added `TransactionPoster` tests (#2557) via NachoSoto (@NachoSoto)
* Refactored `TransactionPoster`: removed 2 dependencies and abstracted
parameters (#2542) via NachoSoto (@NachoSoto)
* `CustomerInfoManagerTests`: wait for `getAndCacheCustomerInfo` to
finish (#2555) via NachoSoto (@NachoSoto)
* `StoreTransaction`: implemented `description` (#2556) via NachoSoto
(@NachoSoto)
* `Backend.ResponseHandler` is now `@Sendable` (#2541) via NachoSoto
(@NachoSoto)
* Extracted `TransactionPoster` from `PurchasesOrchestrator` (#2540) via
NachoSoto (@NachoSoto)
* `enableAdServicesAttributionTokenCollection`: added integration test
(#2551) via NachoSoto (@NachoSoto)
* `AttributionPoster`: replaced hardcoded strings with constants (#2548)
via NachoSoto (@NachoSoto)
* `DefaultDecodable`: moved to `Misc/Codable/DefaultDecodable.swift`
(#2528) via NachoSoto (@NachoSoto)
* `CircleCI`: specify device to run `backend_integration_tests` (#2547)
via NachoSoto (@NachoSoto)
* Created `StoreKit2TransactionFetcher` (#2539) via NachoSoto
(@NachoSoto)
* Fix load shedder integration tests (#2546) via Josh Holtz
(@joshdholtz)
* Fix doc on `Offering.getMetadataValue` (#2545) via Josh Holtz
(@joshdholtz)
* Extracted and tested `AsyncSequence.extractValues` (#2538) via
NachoSoto (@NachoSoto)
* `Offline Entitlements`: don't compute offline `CustomerInfo` when
purchasing a consumable products (#2522) via NachoSoto (@NachoSoto)
* `OfflineEntitlementsManager`: disable offline `CustomerInfo` in
observer mode (#2520) via NachoSoto (@NachoSoto)
* `BasePurchasesTests`: fixed leak detection (#2534) via NachoSoto
(@NachoSoto)
* `PurchaseTesterSwiftUI`: added `ProxyView` to `iOS` (#2531) via
NachoSoto (@NachoSoto)
* `PurchasedProductsFetcher`: removed `AppStore.sync` call (#2521) via
NachoSoto (@NachoSoto)
* `PurchaseTesterSwiftUI`: added new window on Mac to manage proxy
(#2518) via NachoSoto (@NachoSoto)
* `PurchasedProductsFetcher`: added log if fetching purchased products
is slow (#2515) via NachoSoto (@NachoSoto)
* `Offline Entitlements`: disable for custom entitlements mode (#2509)
via NachoSoto (@NachoSoto)
* `Offline Entitlements`: fixed iOS 12 tests (#2514) via NachoSoto
(@NachoSoto)
* `PurchasedProductsFetcher`: don't throw errors if purchased products
were found (#2506) via NachoSoto (@NachoSoto)
* `Offline Entitlements`: allow creating offline `CustomerInfo` with
empty `ProductEntitlementMapping` (#2504) via NachoSoto (@NachoSoto)
* `Offline Entitlements`: integration tests (#2501) via NachoSoto
(@NachoSoto)
* `CustomerInfoManager`: don't cache offline `CustomerInfo` (#2378) via
NachoSoto (@NachoSoto)
* `DangerousSettings`: debug-only `forceServerErrors` (#2486) via
NachoSoto (@NachoSoto)
* `CocoapodsInstallation`: fixed `Xcode 14.3.0` issue (#2489) via
NachoSoto (@NachoSoto)
* `CarthageInstallation`: removed workaround (#2488) via NachoSoto
(@NachoSoto)

---------

Co-authored-by: NachoSoto <ignaciosoto90@gmail.com>
@vegaro vegaro added pr:other and removed pr:perf labels Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants