Skip to content

Commit bd9d56d

Browse files
committed
optimize fetching products from storage when checking products
1 parent d87bac3 commit bd9d56d

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Modules/Sources/Storage/Tools/StorageType+Extensions.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,20 @@ public extension StorageType {
271271
return allObjects(ofType: Product.self, matching: predicate, sortedBy: [descriptor])
272272
}
273273

274+
/// Has stored Products for the provided siteID and optional requirements.
275+
///
276+
func hasProducts(siteID: Int64, status: String?, type: String?) -> Bool {
277+
var predicates: [NSPredicate] = [\Product.siteID == siteID]
278+
if let status {
279+
predicates.append(\Product.statusKey == status)
280+
}
281+
if let type {
282+
predicates.append(\Product.productTypeKey == type)
283+
}
284+
let combinedPredicate = NSCompoundPredicate(type: .and, subpredicates: predicates)
285+
return firstObject(ofType: Product.self, matching: combinedPredicate) != nil
286+
}
287+
274288
/// Retrieves all of the stored Products matching the provided array products ids from the provided SiteID
275289
///
276290
func loadProducts(siteID: Int64, productsIDs: [Int64]) -> [Product] {

Modules/Sources/Yosemite/Stores/ProductStore.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -568,14 +568,8 @@ private extension ProductStore {
568568
onCompletion: @escaping (Result<Bool, Error>) -> Void) {
569569
// Check for locally stored products first.
570570
let storage = storageManager.viewStorage
571-
if let products = storage.loadProducts(siteID: siteID), !products.isEmpty {
572-
if let status, (products.filter { $0.statusKey == status.rawValue }.isEmpty) == false {
573-
return onCompletion(.success(true))
574-
} else if let productType, products.contains(where: { $0.productTypeKey == productType.rawValue}) {
575-
return onCompletion(.success(true))
576-
} else if status == nil && productType == nil {
577-
return onCompletion(.success(true))
578-
}
571+
if storage.hasProducts(siteID: siteID, status: status?.rawValue, type: productType?.rawValue) {
572+
return onCompletion(.success(true))
579573
}
580574

581575
// If there are no locally stored products, then check remote.

0 commit comments

Comments
 (0)