Skip to content

Instantly share code, notes, and snippets.

@olivaresf
Created October 12, 2020 16:22
Show Gist options
  • Save olivaresf/76723b725a1605efde222fbf24732091 to your computer and use it in GitHub Desktop.
Save olivaresf/76723b725a1605efde222fbf24732091 to your computer and use it in GitHub Desktop.
extension UserDefaultsStorage {
/// Previously stored migration data.
///
/// - From the cache, if available. The cache has the same lifetime as the session.
/// - If the cache is not available, try from UserDefaults, save to the cache.
/// - If UserDefaults doesn't have any data, then return `defaultStoreData` and save to UserDefaults.
///
/// - Returns: a struct describing model version, settings bundle and onboarding bundle.
private var storedMigrationData: MigrationData {
// Do we have a cache?
guard let cache = storedDataCache else {
let defaultMigrationData = MigrationData(modelVersion: modelVersion,
settingsBundle: .default,
onboardingBundle: .default)
// We do not. Do we have the previous data in UserDefaults?
guard let savedJSONData = UserDefaults.standard.value(forKey: key) as? Data else {
// We don't have previous data.
print("No data found for key \(key), generating default values")
storedDataCache = defaultMigrationData
save(storedData: storedDataCache!)
return storedDataCache!
}
// We do have the previous data in UserDefaults.
// Transform it into a JSON.
//
// `storedData` should be a variable because we're using it as fallback.
var storedData = defaultMigrationData
do {
storedData = try JSONDecoder().decode(MigrationData.self, from: savedJSONData)
} catch {
print("error reading stored data \(error), generating default values")
}
storedDataCache = storedData
return storedData
}
return cache
}
#warning("Can we make this function throw?")
private func save(storedData: MigrationData) {
storedDataCache = storedData
do {
try UserDefaults.standard.set(JSONEncoder().encode(storedData), forKey: key)
} catch {
print("failed saving data :", error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment