Reputation: 7455
If a new version is not backward compatible with previous version's data, what is the best way to ensure NSUserDefaults
is deleted upon upgrade?
Upvotes: 0
Views: 308
Reputation: 18122
A simple way would be to keep a default that tracks whether the upgrade process has been performed. The new version would check for it, and if it hasn't been set, perform the upgrade procedure and set the value to make sure the process isn't performed twice. Like this:
static NSString * const kUserDefaultsDidUpgradeKey = @"didUpgrade";
NSUserDefaults *ud = NSUserDefaults.standardUserDefaults;
if (![ud boolForKey:kUserDefaultsDidUpgradeKey]) {
// Delete the keys here
[ud setBool:YES forKey:kUserDefaultsDidUpgradeKey];
}
However, assuming that you would want to handle other version migrations in the future, a more robust way would be to write the app version to user defaults so that when the app is updated, the new version can check the version key to see what the previous version was, and run the upgrade process accordingly.
Upvotes: 4