Have you enabled Persistent Store migration? As long as you are only making trivial model modifications, automatic migration will mean you don't need to reset the data.
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:@{
NSMigratePersistentStoresAutomaticallyOption:@YES,
NSInferMappingModelAutomaticallyOption:@YES,
NSSQLiteAnalyzeOption:@YES,
}
error:&error])
If there's a different reason to clear your model, then NSUserDefaults
is the way to go. You can use a #define to set a version number, and have the persistent store reset when the numbers are different, then when you know you've made a change that requires an empty store you can increment the #define:
#define kNukeTheCache 1000
...
NSNumber *nukeNumber = [[NSUserDefaults standardUserDefaults] objectForKey:nukemeKey];
if ((nukeNumber == nil) || (nukeNumber.integerValue < kNukeTheCache)) {
[self deletePersistentStore:storeURL];
}
[[NSUserDefaults standardUserDefaults] setInteger:kNukeTheCache forKey:nukemeKey];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:totalNuke];
[[NSUserDefaults standardUserDefaults] synchronize];
- (void)deletePersistentStore:(NSURL *)storeURL
{
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
if (error) { // Handle error }
}