Search code examples
iphoneioscore-dataxcode4.2core-data-migration

Cannot create an NSPersistentStoreCoordinator with a nil model error


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


Solution

  • 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...