问题
I have a coreData datamodel file which was running perfectly. Due to some special requirements i deleted the old datamodel file and created another datamodel file with exactly same entities. There is no change in entities from the previous dataModel. I have made this a part of a different bundle and referring it from that bundle.
Code for creating the managedObjectModel
if (managedObjectModel_ != nil) {
return managedObjectModel_;
}
NSBundle *newBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"dataBundle" withExtension:@"bundle"]];
NSString *modelPath = [newBundle pathForResource:@"DataHouse" ofType:@"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel_;
The app is running fine till some time and suddenly (randomly) i get an error saying
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'
*** First throw call stack:`(0x62e052 0x26a9d0a 0xf6e86d 0x64fd 0x624e 0x381b 0x79c9b 0x65f2d 0x1881e0f 0x1882589 0x186ddfd 0x187c851 0x1827322 0x62fe72 0x160892d 0x1612827 0x1598fa7 0x159aea6 0x163437a 0x16341af 0x602966 0x602407 0x5657c0 0x564db4 0x564ccb 0x2791879 0x279193e 0x17e8a9b 0x28a2 0x2815)`
The code for creating the persistant store coordinator
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"DataHouse.sqlite"]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator_;
This error occurs at a random point but is very much consistent every time i run the app. I am totally confused and struck at this point... I have seen the same problem in the forum but i guess mine is a special scenario. I am pretty sure that the modelPath variable mentioned in the above code snippet is coming fine every time i print it.
NOTE : the above code snippets are not a part of the AppDelegate class. They are a part of a special class that contains all Coredata methods
回答1:
First verify that managedObjectModel_ is valid, you can use the debugger or call a method on it like
NSLog(@"%@", [managedObjectModel_ entities]);
to verify that your data model is fine. That call should show an array of all your Entities in the model. Next check that your persistent store path is pointing at the correct location. Try this:
NSLog(@"%@", [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"DataHouse.sqlite"]);
Finally (which I think is what needs to be done because it's happened to me) go to the location on the disk where the .sqlite file is stored:
~/Library/Application Support/iPhone Simulator/5.0/Applications/{your app id}/Documents
and delete the .sqlite file so Core Data will generate it fresh the next time you build.
回答2:
The actual problem turned out to be a memory issue. I was not using core data objects from AppDelegate and i am creating them in some other classes. But i have not released these objects due to which there was a huge leak and the app got crashed. I released the core data objects and now my app is working perfectly fine...
回答3:
if your datamodel name is Model.xcdatamodeid. just replace the URLForResource parameter as your datamodel name in AppDelegate.h file.
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
below code for reference.
(NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error
for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
来源:https://stackoverflow.com/questions/8804541/cannot-create-an-nspersistentstorecoordinator-with-a-nil-model-error