I have a Struct for the user's preferences and this object gets encoded and stored as a JSON file and stored for iCloud Document Sync (I found this to be more reliable than NSUbiquitousContainer, even though that's what you're supposed to use for preferences).
Let's say in v1 of the app the struct looks like this and all users have a JSON file with these 3 properties.
struct Preferences: Codable {
var soundsDisabled: Bool = false
var hapticsDisabled: Bool = false
var badgeNumberEnabled: Bool = false
}
What I need to figure out is if there is a way to add a 4th property in v1.1 to this Struct without messing up the existing JSON. Right now if I do add a new property, everything gets reset to the default values and the user's preferences get lost.
UPDATE: this is how this struct gets encoded to JSON
extension Storage {
static var preferences: Preferences {
get {
guard
let data = try? Data(contentsOf: Storage.preferencesFile),
let decoded = try? JSONDecoder().decode(Preferences.self, from: data)
else { return Preferences() }
return decoded
}
set {
do { try JSONEncoder().encode(newValue).write(to: Storage.preferencesFile, options: .atomic) }
catch let error {
print("Failed to write Preferences: \(error.localizedDescription)")
MSAnalytics.trackEvent("Failed to write preferences", withProperties: ["Error": error.localizedDescription])
}
}
}
}
Thank you.