Skip to content

Commit

Permalink
Merge pull request #2 from wiencheck/master
Browse files Browse the repository at this point in the history
Return cached products if requested identifiers match
  • Loading branch information
tikhop authored Jan 13, 2022
2 parents 0165867 + 0289bd1 commit 1df738d
Showing 1 changed file with 19 additions and 2 deletions.
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
}
}

0 comments on commit 1df738d

Please sign in to comment.