From 1d62c5c9bd76162533977956527877a16a4b9e75 Mon Sep 17 00:00:00 2001 From: Albert Wu Date: Thu, 29 Mar 2018 13:30:46 -0700 Subject: [PATCH] Allow custom UserDefaults object, bug fixed (#28) * Allowed custom UserDefaults object * Passed optional parameter to overloaded function sync(keys: keys) => sync(userDefaults: userDefaults, keys: keys) * Trimmed whitespaces in blank lines * Update Zephyr.swift * Matched whitespaces * Added sync func for custom UserDefaults Added 3rd and 4th overloaded sync func, with a custom UserDefaults object as an optional parameter, rather than adding the optional parameter to the original functions. --- Sources/Zephyr.swift | 61 +++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/Sources/Zephyr.swift b/Sources/Zephyr.swift index 7da2202..ef2767d 100644 --- a/Sources/Zephyr.swift +++ b/Sources/Zephyr.swift @@ -46,7 +46,7 @@ public class Zephyr: NSObject { /// A session-persisted variable to directly access all of the NSUserDefaults elements. private var zephyrLocalStoreDictionary: [String: Any] { - return UserDefaults.standard.dictionaryRepresentation() + return userDefaults.dictionaryRepresentation() } /// A session-persisted variable to directly access all of the NSUbiquitousKeyValueStore elements. @@ -54,6 +54,9 @@ public class Zephyr: NSObject { return NSUbiquitousKeyValueStore.default.dictionaryRepresentation } + //The UserDefaults object to sync with NSUbiquitousKeyValueStore/iCloud + private var userDefaults: UserDefaults = UserDefaults.standard + /// Zephyr's initialization method. /// /// Do not call this method directly. @@ -72,7 +75,7 @@ public class Zephyr: NSObject { deinit { zephyrQueue.sync { for key in registeredObservationKeys { - UserDefaults.standard.removeObserver(self, forKeyPath: key) + userDefaults.removeObserver(self, forKeyPath: key) } } } @@ -86,6 +89,7 @@ public class Zephyr: NSObject { /// - Parameters: /// - keys: If you pass a one or more keys, only those key will be synchronized. If no keys are passed, than all NSUserDefaults will be synchronized with NSUbiquitousKeyValueStore. public static func sync(keys: String...) { + if !keys.isEmpty { sync(keys: keys) return @@ -111,9 +115,10 @@ public class Zephyr: NSObject { /// /// This method will synchronize an array of keys between NSUserDefaults and NSUbiquitousKeyValueStore. /// - /// - Parameters: + /// - Parameters: /// - keys: An array of keys that should be synchronized between NSUserDefaults and NSUbiquitousKeyValueStore. public static func sync(keys: [String]) { + switch shared.dataStoreWithLatestData() { case .local: printGeneralSyncStatus(finished: false, destination: .remote) @@ -130,6 +135,32 @@ public class Zephyr: NSObject { } } + /// Overloaded version of Zephyr's synchronization method, **sync(keys:)**. + /// + /// If a custom UserDefaults object is passed in, Zephyr will synchronize that rather than UserDefaults.standard + /// + /// - Parameters: + /// - userDefaults: The UserDefaults object that should be synchronized with UbiquitousKeyValueStore.default + /// default value is UserDefaults.standard + /// - keys: If you pass a one or more keys, only those key will be synchronized. If no keys are passed, than all NSUserDefaults will be synchronized with NSUbiquitousKeyValueStore. + public static func sync(keys: String..., userDefaults: UserDefaults = UserDefaults.standard) { + shared.userDefaults = userDefaults + sync(keys: keys) + } + + /// Overloaded version of Zephyr's synchronization method, **sync(keys:)**. + /// + /// If a custom UserDefaults object is passed in, Zephyr will synchronize that rather than UserDefaults.standard + /// + /// - Parameters: + /// - userDefaults: The UserDefaults object that should be synchronized with UbiquitousKeyValueStore.default + /// default value is UserDefaults.standard + /// - keys: An array of keys that should be synchronized between NSUserDefaults and NSUbiquitousKeyValueStore. + public static func sync(keys: [String], userDefaults: UserDefaults = UserDefaults.standard) { + shared.userDefaults = userDefaults + sync(keys: keys) + } + /// Add specific keys to be monitored in the background. Monitored keys will automatically /// be synchronized between both data stores whenever a change is detected /// @@ -178,7 +209,7 @@ public class Zephyr: NSObject { /// /// Remove specific keys from being monitored in the background. /// - /// - Parameters: + /// - Parameters: /// - keys: Pass one or more keys that you would like to stop monitoring. public static func removeKeysFromBeingMonitored(keys: String...) { removeKeysFromBeingMonitored(keys: keys) @@ -227,8 +258,8 @@ private extension Zephyr { private extension Zephyr { /// Synchronizes specific keys to/from NSUbiquitousKeyValueStore and NSUserDefaults. - /// - /// - Parameters: + /// + /// - Parameters: /// - keys: Array of keys to synchronize. /// - dataStore: Signifies if keys should be synchronized to/from iCloud. func syncSpecificKeys(keys: [String], dataStore: ZephyrDataStore) { @@ -247,8 +278,8 @@ private extension Zephyr { /// Synchronizes all NSUserDefaults to NSUbiquitousKeyValueStore. /// /// If a key is passed, only that key will be synchronized. - /// - /// - Parameters: + /// + /// - Parameters: /// - key: If you pass a key, only that key will be updated in NSUbiquitousKeyValueStore. /// - value: The value that will be synchronized. Must be passed with a key, otherwise, nothing will happen. func syncToCloud(key: String? = nil, value: Any? = nil) { @@ -295,7 +326,7 @@ private extension Zephyr { /// - key: If you pass a key, only that key will updated in NSUserDefaults. /// - value: The value that will be synchronized. Must be passed with a key, otherwise, nothing will happen. func syncFromCloud(key: String? = nil, value: Any? = nil) { - let defaults = UserDefaults.standard + let defaults = userDefaults defaults.set(Date(), forKey: ZephyrSyncKey) // Sync all defaults from iCloud if key is nil, otherwise sync only the specific key/value pair. @@ -331,7 +362,7 @@ extension Zephyr { /// Adds key-value observation after synchronization of a specific key. /// - /// - Parameters: + /// - Parameters: /// - key: The key that should be added and monitored. private func registerObserver(key: String) { if key == ZephyrSyncKey { @@ -340,7 +371,7 @@ extension Zephyr { if !registeredObservationKeys.contains(key) { - UserDefaults.standard.addObserver(self, forKeyPath: key, options: .new, context: nil) + userDefaults.addObserver(self, forKeyPath: key, options: .new, context: nil) registeredObservationKeys.append(key) } @@ -359,7 +390,7 @@ extension Zephyr { if let index = registeredObservationKeys.index(of: key) { - UserDefaults.standard.removeObserver(self, forKeyPath: key, context: nil) + userDefaults.removeObserver(self, forKeyPath: key, context: nil) registeredObservationKeys.remove(at: index) } @@ -376,7 +407,7 @@ extension Zephyr { zephyrQueue.async { if self.registeredObservationKeys.contains(keyPath) { if object is UserDefaults { - UserDefaults.standard.set(Date(), forKey: self.ZephyrSyncKey) + self.userDefaults.set(Date(), forKey: self.ZephyrSyncKey) } self.syncSpecificKeys(keys: [keyPath], dataStore: .local) @@ -458,7 +489,7 @@ private extension Zephyr { /// Prints the subscription state for a specific key if debugEnabled == true /// - /// - Parameters: + /// - Parameters: /// - key: The key being synchronized. /// - subscribed: The subscription status of the key. static func printObservationStatus(key: String, subscribed: Bool) { @@ -473,7 +504,7 @@ private extension Zephyr { /// Prints a status to the console if /// - /// - Parameters: + /// - Parameters: /// - debugEnabled == true /// - status: The string that should be printed to the console. static func printStatus(status: String) {