I'd like to recreate (drop & create) persistentStore not to be affected by changing .xcdatamodeld.
I wrote a code persistentStoreCoordinator in AppDelegate likes below:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"myproject.sqlite"];
// delete if database exists
NSError *error = nil;
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// if .xcdatamodeld is changed, fail and in here...
// if not changed, recreate success. all data removed from database
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
NSArray *stores = [_persistentStoreCoordinator persistentStores];
for (NSPersistentStore *store in stores) {
[_persistentStoreCoordinator removePersistentStore:store error:nil];
[[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:nil];
}
// newly create database
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
when I make a change to .xcdatamodeld
(ex. adding new column to entity) and reboot simulator, then fail at first addPersistentStoreWithType
and log says
Unresolved error Error Domain=NSCocoaErrorDomain Code=134100
The operation couldn’t be completed. (Cocoa error 134100.)
How can I do that?
The simplest way to solve your problem is to handle this error the following way: delete your DB file and try again. It can do for testing period.
However, if you need a stable solution, use model with version and automatic migrations:
Also, if automigrations are not enough, you should provide migration mapping.
If you want just to delete the store use the file manager like that: