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

Core Data: Migrate storage usage in SettingStore #14569

Merged
merged 5 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Internal] Updated storage usage in CouponStore [https://github.com/woocommerce/woocommerce-ios/pull/14530]
- [Internal] Update storage usage for BlazeStore [https://github.com/woocommerce/woocommerce-ios/pull/14532]
- [Internal] Updated storage usage in ProductShippingClassStore [https://github.com/woocommerce/woocommerce-ios/pull/14520]
- [Internal] updated storage usage in SettingStore [https://github.com/woocommerce/woocommerce-ios/pull/14569]
- [*] Fixed: Improved the error message displayed when Bluetooth permission is denied during the card reader connection process. [https://github.com/woocommerce/woocommerce-ios/pull/14561]

21.2
Expand Down
42 changes: 12 additions & 30 deletions Yosemite/Yosemite/Stores/SettingStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ public class SettingStore: Store {
private let siteSettingsRemote: SiteSettingsRemote
private let siteAPIRemote: SiteAPIRemote

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

public override init(dispatcher: Dispatcher, storageManager: StorageManagerType, network: Network) {
self.siteSettingsRemote = SiteSettingsRemote(network: network)
self.siteAPIRemote = SiteAPIRemote(network: network)
Expand Down Expand Up @@ -196,48 +192,34 @@ private extension SettingStore {
/// on the main thread!
///
func upsertStoredGeneralSettingsInBackground(siteID: Int64, readOnlySiteSettings: [Networking.SiteSetting], onCompletion: @escaping () -> Void) {
let derivedStorage = sharedDerivedStorage
derivedStorage.perform {
self.upsertSettings(readOnlySiteSettings, in: derivedStorage, siteID: siteID, settingGroup: SiteSettingGroup.general)
}

storageManager.saveDerivedType(derivedStorage: derivedStorage) {
DispatchQueue.main.async(execute: onCompletion)
}
storageManager.performAndSave({ [weak self] storage in
self?.upsertSettings(readOnlySiteSettings, in: storage, siteID: siteID, settingGroup: SiteSettingGroup.general)
}, completion: onCompletion, on: .main)
}

/// Updates (OR Inserts) the specified **product** ReadOnly `SiteSetting` entities **in a background thread**. `onCompletion` will be called
/// on the main thread!
///
func upsertStoredProductSettingsInBackground(siteID: Int64, readOnlySiteSettings: [Networking.SiteSetting], onCompletion: @escaping () -> Void) {
let derivedStorage = sharedDerivedStorage
derivedStorage.perform {
self.upsertSettings(readOnlySiteSettings, in: derivedStorage, siteID: siteID, settingGroup: SiteSettingGroup.product)
}

storageManager.saveDerivedType(derivedStorage: derivedStorage) {
DispatchQueue.main.async(execute: onCompletion)
}
storageManager.performAndSave({ [weak self] storage in
self?.upsertSettings(readOnlySiteSettings, in: storage, siteID: siteID, settingGroup: SiteSettingGroup.product)
}, completion: onCompletion, on: .main)
}

/// Updates (OR Inserts) the specified **advanced** ReadOnly `SiteSetting` entities **in a background thread**. `onCompletion` will be called
/// on the main thread!
///
func upsertStoredAdvancedSettingsInBackground(siteID: Int64, readOnlySiteSettings: [Networking.SiteSetting], onCompletion: @escaping () -> Void) {
let derivedStorage = sharedDerivedStorage
derivedStorage.perform {
self.upsertSettings(readOnlySiteSettings, in: derivedStorage, siteID: siteID, settingGroup: SiteSettingGroup.advanced)
}

storageManager.saveDerivedType(derivedStorage: derivedStorage) {
DispatchQueue.main.async(execute: onCompletion)
}
storageManager.performAndSave({ [weak self] storage in
self?.upsertSettings(readOnlySiteSettings, in: storage, siteID: siteID, settingGroup: SiteSettingGroup.advanced)
}, completion: onCompletion, on: .main)
}

func upsertSettings(_ readOnlySiteSettings: [SiteSetting], in storage: StorageType, siteID: Int64, settingGroup: SiteSettingGroup) {
let storageSiteSettings = storage.loadSiteSettings(siteID: siteID, settingGroupKey: settingGroup.rawValue)
// Upsert the settings from the read-only site settings
for readOnlyItem in readOnlySiteSettings {
if let existingStorageItem = storage.loadSiteSetting(siteID: siteID, settingID: readOnlyItem.settingID) {
if let existingStorageItem = storageSiteSettings?.first(where: { $0.settingID == readOnlyItem.settingID }) {
existingStorageItem.update(with: readOnlyItem)
} else {
let newStorageItem = storage.insertNewObject(ofType: Storage.SiteSetting.self)
Expand All @@ -246,7 +228,7 @@ private extension SettingStore {
}

// Now, remove any objects that exist in storageSiteSettings but not in readOnlySiteSettings
if let storageSiteSettings = storage.loadSiteSettings(siteID: siteID, settingGroupKey: settingGroup.rawValue) {
if let storageSiteSettings {
storageSiteSettings.forEach({ storageItem in
if readOnlySiteSettings.first(where: { $0.settingID == storageItem.settingID } ) == nil {
storage.deleteObject(storageItem)
Expand Down
Loading