I just released an app to the app store and one of my patrons let me know that I should change the data type that I was previously storing as an Integer, using the NSKeyedArchiver, to a Double.
Easy enough to change the app's data model but when I reload the app on my test device, the NSKeyedUnarchiver obviously doesn't want to decode an Integer as a Double and throws an NSInvalidUnarchiveOperation Exception.
I was wondering how any other iOS Dev's would handle this situation. I'd hate to erase all of my users' previously saved data but that's the only solution I'm seeing.
My code is posted below. I've commented out a few solutions I tried to no avail
required convenience init?(coder aDecoder: NSCoder){
func decodeDoubles(coder aDecoder: NSCoder) throws-> (Double, Double){
print("Getting in here")
/* These are stored as Integers in previous version */
let myEarned = aDecoder.decodeDoubleForKey(PropertyKey.earnedKey)
let myTotal = aDecoder.decodeDoubleForKey(PropertyKey.totalKey)
/* App Crashes here - exception not caught */
print("After decode attempt")
return (myEarned, myTotal)
}
let myID = aDecoder.decodeIntegerForKey(PropertyKey.idKey)
let myName = aDecoder.decodeObjectForKey(PropertyKey.nameKey) as! String
let myWeight = aDecoder.decodeIntegerForKey(PropertyKey.weightKey)
/* Throws exception */
//let myEarned = aDecoder.decodeDoubleForKey(PropertyKey.earnedKey)
//let myTotal = try! aDecoder.decodeDoubleForKey(PropertyKey.totalKey)
var myEarned: Double = 0
var myTotal: Double = 0
do {
(myEarned, myTotal) = try decodeDoubles(coder: aDecoder)
} catch {
print("Exception caught - \(error)")
myEarned = Double(aDecoder.decodeIntegerForKey(PropertyKey.earnedKey))
myTotal = Double(aDecoder.decodeIntegerForKey(PropertyKey.totalKey))
}
self.init(id: myID, name: myName, weight: myWeight, earned: myEarned, total: myTotal)
}
You might need to make a function to sort of upgrade the archive, as the app loads, read in the key as an Integer, and write it back as a double, then the rest of your app can read and write it as a double normally. you will need a new key to flag that you have done the upgrade so you dont do it again, and dont do it for new users.