Problem with context in recreating CoreData
I need to delete all user info and recreate it. The code i use to create everything is in the app delegate:
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString* dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
开发者_如何学Python NSUserDomainMask, YES) lastObject];
NSURL *storeURL = [NSURL fileURLWithPath: [dir stringByAppendingPathComponent:
@"Data.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
Then, i delete everything with:
[context reset];
NSString* dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) lastObject];
NSURL *storeURL = [NSURL fileURLWithPath: [dir stringByAppendingPathComponent:
@"Data.sqlite"]];
NSError *error;
NSURL *storeURL = store.URL;
NSPersistentStoreCoordinator *storeCoordinator = [((CustomAppDelegate *)[[UIApplication sharedApplication] delegate]) persistentStoreCoordinator];
NSPersistentStore *store = [[storeCoordinator persistentStores] objectAtIndex:0];
[storeCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
[storeCoordinator release];
storeCoordinator = nil;
And it seems to work, the problem is when i try to save the new data, afterwards. If i do something like:
NSManagedObjectContext *afterContext = [appDelegate managedObjectContext];
NSEntityDescription *userEntity = [NSEntityDescription entityForName:@"User"
inManagedObjectContext:afterContext];
NSManagedObject *user = [NSEntityDescription
insertNewObjectForEntityForName:[userEntity name] inManagedObjectContext:afterContext];
[user setValue:@"John" forKey:@"firstName"];
if(![afterContext save:&error])
NSLog(@"oops");
it crashes in afterContext save, it gaves me a EXC_BAD_INSTUCTION exception. When i do [appDelegate managedObjectContext] it creates the new persistentStore, so i don't know what could be the problem. A reference to a previous object? The stacktrace does not give any clue at all.
When i do [appDelegate managedObjectContext] it creates the new persistentStore
That is correct but only if one does not already exist. You can not "take" an object that you do not own, release it and then set it to nil and expect it to work. The line where you are setting storeCoordinator to the app delegates coordinator is just setting the pointer value. When you release that coordinator at the end and set it to nil the AppDelegate is still set to the address of the now deallocated coordinator. That is the reason for your crash. You will need to come up with a way of releasing and nil'ing the value in the actual AppDelegate itself. Also the managed object context in the appDelegate will need to updated to reflect the changes.
精彩评论