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

Return cached products if requested identifiers match #2

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Sources/Mercato/ProductService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ import StoreKit

class ProductService
{
private var cachedProducts: [Product] = []

private var cachedProducts: [Product] = []

@MainActor
public func retrieveProducts(productIds: Set<String>) async throws -> [Product]
{
do
{
if checkProductIdsMatchingCachedIds(productIds) {
return cachedProducts
}
let products = try await Product.products(for: productIds)
cachedProducts = products

return products
}catch{
} catch {
throw MercatoError.storeKit(error: error as! StoreKitError)
}
}
Expand All @@ -41,3 +46,15 @@ class ProductService
return transaction.revocationDate == nil && !transaction.isUpgraded
}
}

private extension ProductService
{
func checkProductIdsMatchingCachedIds(_ productIds: Set<String>) -> Bool {
if cachedProducts.isEmpty {
return false
}

let cachedProductIds = Set(cachedProducts.map(\.id))
return cachedProductIds == productIds
}
}