Skip to content

Commit

Permalink
[Core Data] Optimize storage usage for product shipping class (#14520)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmeichigo authored Nov 29, 2024
2 parents dec5261 + d6f1df4 commit 3c5694c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [*] Jetpack Setup: Fixed an issue with magic link handling when the app is cold started. [https://github.com/woocommerce/woocommerce-ios/pull/14502]
- [**] Media Library: On sites logged in with application password, when picking image from WordPress Media Library, all images will now load correctly. [https://github.com/woocommerce/woocommerce-ios/pull/14444]
- [Internal] Updated storage usage in CouponStore [https://github.com/woocommerce/woocommerce-ios/pull/14530]
- [Internal] Updated storage usage in ProductShippingClassStore [https://github.com/woocommerce/woocommerce-ios/pull/14520]

21.2
-----
Expand Down
44 changes: 15 additions & 29 deletions Yosemite/Yosemite/Stores/ProductShippingClassStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import Storage
public final class ProductShippingClassStore: Store {
private let remote: ProductShippingClassRemote

private lazy var sharedDerivedStorage: StorageType = {
return storageManager.writerDerivedStorage
}()

public override init(dispatcher: Dispatcher, storageManager: StorageManagerType, network: Network) {
self.remote = ProductShippingClassRemote(network: network)
super.init(dispatcher: dispatcher, storageManager: storageManager, network: network)
Expand Down Expand Up @@ -56,14 +52,12 @@ private extension ProductShippingClassStore {
return
}

if pageNumber == Default.firstPageNumber {
self.deleteStoredProductShippingClassModels(siteID: siteID)
}

let shouldDeleteAllItems = pageNumber == Default.firstPageNumber
self.upsertStoredProductShippingClassModelsInBackground(readOnlyProductShippingClassModels: models,
siteID: siteID) {
let hasNextPage = models.count == pageSize
onCompletion(.success(hasNextPage))
siteID: siteID,
shouldDeleteAllStoredItems: shouldDeleteAllItems) {
let hasNextPage = models.count == pageSize
onCompletion(.success(hasNextPage))
}
}
}
Expand All @@ -84,14 +78,6 @@ private extension ProductShippingClassStore {
}
}
}

/// Deletes any Storage.ProductShippingClass with the specified `siteID` and `productID`
///
func deleteStoredProductShippingClassModels(siteID: Int64) {
let storage = storageManager.viewStorage
storage.deleteProductShippingClasses(siteID: siteID)
storage.saveIfNeeded()
}
}


Expand All @@ -104,16 +90,16 @@ private extension ProductShippingClassStore {
///
func upsertStoredProductShippingClassModelsInBackground(readOnlyProductShippingClassModels: [Networking.ProductShippingClass],
siteID: Int64,
shouldDeleteAllStoredItems: Bool = false,
onCompletion: @escaping () -> Void) {
let derivedStorage = sharedDerivedStorage
derivedStorage.perform { [weak self] in
storageManager.performAndSave({ [weak self] storage in
if shouldDeleteAllStoredItems {
storage.deleteProductShippingClasses(siteID: siteID)
}
self?.upsertStoredProductShippingClassModels(readOnlyProductShippingClassModels: readOnlyProductShippingClassModels,
in: derivedStorage, siteID: siteID)
}

storageManager.saveDerivedType(derivedStorage: derivedStorage) {
DispatchQueue.main.async(execute: onCompletion)
}
in: storage,
siteID: siteID)
}, completion: onCompletion, on: .main)
}
}

Expand All @@ -129,10 +115,10 @@ private extension ProductShippingClassStore {
func upsertStoredProductShippingClassModels(readOnlyProductShippingClassModels: [Networking.ProductShippingClass],
in storage: StorageType,
siteID: Int64) {
let storedItems = storage.loadProductShippingClasses(siteID: siteID)
// Upserts the ProductShippingClass models from the read-only version
for readOnlyProductShippingClass in readOnlyProductShippingClassModels {
let storageProductShippingClass = storage.loadProductShippingClass(siteID: siteID,
remoteID: readOnlyProductShippingClass.shippingClassID)
let storageProductShippingClass = storedItems?.first(where: { $0.shippingClassID == readOnlyProductShippingClass.shippingClassID })
?? storage.insertNewObject(ofType: Storage.ProductShippingClass.self)
storageProductShippingClass.update(with: readOnlyProductShippingClass)
}
Expand Down

0 comments on commit 3c5694c

Please sign in to comment.