96

Been having my first crack at Core Data and I'm getting the following error when running my code on my device, but it works fine on the simulator..

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'

Some of my methods that might be causing the problem:

    - (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil)
    {
        return __managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return __managedObjectContext;
}

/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created from the application's model.
 */
- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil)
    {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"RugbyOnTv" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
    return __managedObjectModel;
}

/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"RugbyOnTV.sqlite"];

    NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];    
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

    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;
}


    - (NSString *)applicationDocumentsDirectory {

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        return basePath;
    }

EDIT

I copied and pasted the managedObjectContext method (below) from Apple's CoreDataBooks and it now works..Not really sure why though

- (NSManagedObjectModel *)managedObjectModel {
    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}
javajavajavajavajava
  • 1,223
  • 2
  • 12
  • 20
Dominic Williams
  • 1,538
  • 2
  • 13
  • 17
  • Hey it could be as simple as appending the word "Model" to the first URLForResource parameter....yeah, I had the same problem. Then I checked the actual .app contents in the command line and found out that the .momd was actually being created. So try this: [[NSBundle mainBundle] URLForResource:@"RugbyOnTvModel" withExtension:@"momd"]; – PostCodeism Sep 17 '12 at 12:27
  • `NSString *basePath = [paths firstObject];` – William Entriken Apr 09 '14 at 21:59

27 Answers27

156

I had exactly the same error message as the original post. I was wrestling with this for hours. It was this line in my AppDelegate.m.

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"[same with name of xcdatamodeld]" withExtension:@"momd"];

For anyone out there searching this error message and finding this thread....try this first.

You must make sure that where it says [same with name of xcdatamodeld]....that it is!!! For some reason, mine had my project name in there and not the data model name.

Changed it and it worked straight away.....

Thanks to Rock & Muller for contributing.......you saved me days!!

Gaz.

rohan-patel
  • 5,772
  • 5
  • 45
  • 68
Gareth Lloyd
  • 1,621
  • 1
  • 10
  • 7
54

first verify:

NSLog(@"%@", [self managedObjectModel]);

If you get a nil value maybe the problem is here

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"RugbyOnTv" withExtension:@"momd"];

So, try changing @"momd" by @"mom"

D33pN16h7
  • 2,030
  • 16
  • 20
  • 1
    Your right. It's null and so this line is causing the error: ` __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];` Changing to mom doesn't seem to fix anything though – Dominic Williams Sep 09 '11 at 12:55
  • 3
    Most likely `modelURL` is also `nil`. The most common cause is a typo in `RugbyOnTv`. Note that this is case-sensitive. – Rob Napier Sep 09 '11 at 13:18
  • I agree, the file name of model is case sensitive. – D33pN16h7 Sep 09 '11 at 13:51
  • I edited my question (can't add my own answer yet). Do you Rob or @D33pN16h7 know why that change has fixed it and what was causing the issue? Was I meant to have created that .momd file at some point? – Dominic Williams Sep 09 '11 at 15:32
  • You have added versioning support to your model, those lines makes a merge between versions, and "build" a model from them. – D33pN16h7 Sep 09 '11 at 16:50
  • 1
    Changing "momd" to "mom" worked for me too. Thanks. But.. WHY? Why is XCode generating code that doesn't work? Why do I have find obscure fixes like this in SO to get the basic templates working? – Rhubarb Sep 13 '11 at 14:52
  • I'd like to know what is going on with this too. This fixed it for me but I don't know why. – Gerald Oct 22 '11 at 10:06
  • 10
    Having recently introduced a 2nd version of my model I had to change it back to momd. Given that the model version container has an extension of xdatamodeld I think we can infer what is going on here. "momd" is for models with more than one version while "mom" is for models that do not have versioning. – Gerald Nov 26 '11 at 04:27
  • I was initially specifying the URL like so: `NSString *managedObjectModelPath = [[NSBundle mainBundle] pathForResource:@"MyProj" ofType:@"momd"];` and then converting it to a URL, but changing it to `NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyProj" withExtension:@"momd"];` worked for me. – Stunner Mar 05 '12 at 09:08
  • 2
    I copied code into a new project [iOS7] verbatim and it was originally momd but changing to mom fixed it. I have no idea how I would have found a solution without this, so thanks :) – Ian Clay Jul 25 '13 at 11:18
20

I've experienced a weird issue with Xcode 4.3.2 and iOS 5.

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"NAME_OF_THE_MODEL" withExtension:@"momd"];

returns a valid URL but

__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

returns a null NSManagedObjectModel. But after checking the documentation, it seems like NSManagedObjectModel needs a file where as NAME_OF_THE_MODEL.momd is a directory which contains a NAME_OF_THE_MODEL.mom file. Changing the URL to

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"NAME_OF_THE_MODEL" withExtension:@"mom" subdirectory:@"NAME_OF_THE_MODEL.momd"];

then works. Seems weird though that Xcode generates code that doesn't work with itself...

xster
  • 6,269
  • 9
  • 55
  • 60
  • 1
    This specific issue is caused by creating an extra model version and then attempting to delete it manually while Xcode is open. This causes some kind of corruption. You should not be passing in specific model versions in the momd directory. – Mike Weller Jul 16 '12 at 08:23
  • cool @MikeWeller, good to know. Do you know where's the metadata that points to the mom file in the momd directory? – xster Jul 16 '12 at 19:15
  • I wasn't trying to delete it, but I had this issue and the solution posted worked for me, specifically, I just added a check to see if its still nil and if it is I added that code above: if (_managedObjectContext == nil) { NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"mom" subdirectory:@"Model.momd"]; _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; } – David van Dugteren Nov 26 '12 at 03:14
19

I had this problem and changing "moms" to "mom" didn't do anything. To fix it I had to right click on the xcdatamodelId file > show package contents then delete the hidden .xcurrentversion file.

P.S: This problem only started happening after I'd changed the name of the data model file.

JDx
  • 2,615
  • 3
  • 22
  • 33
  • 2
    For everybody encountering the following problem: managedObjectModel being nil after renaming the xcdatamodeld file: this answer is the best! Thanks JDx, your answer helped me a lot! – Dumoko Sep 20 '12 at 06:41
  • Instead of deleting it you may eventually edit it in order to change the filename stored to the latest version of youl xcdatamodel – furins Jun 24 '14 at 17:18
13

Another source of this error, is that sometimes Xcode doesn't include the data model in the build.

Check the Build Phases of your target, and ensure that the *.xcdatamodeld file is included in the Compile Sources section.

Dave Wood
  • 13,143
  • 2
  • 59
  • 67
9

What's probably happened is that your xcdatamodeld file from Apple's source code has turned into a xcdatamodel(without the d) file and therefore they aren't considered the same.

The quickest way to fix this, is to select the xcdatamodel file in your project navigator and in to the menu bar

Editor->Add Model Version...

and add a new version of your model. Make all changes to the new version.

This works in Xcode 5

Louis Cremen
  • 764
  • 7
  • 12
  • unfortunately this option doesn't exist in Xcode 5.. see [here](http://s30.postimg.org/p3495g9ch/Screen_Shot_2014_02_01_at_10_18_07_AM.png) – abbood Feb 01 '14 at 08:21
  • Forgot to mention that you need to select the xcdatamodel file. Edited answer to reflect comment. – Louis Cremen Feb 05 '14 at 02:48
9

I solve it by adding the db file to the Copy Bundle Resources.

Go to root of your project >> select your target >> Build Phases >> Copy Bundle Resources . Make sure your xcdatamodeld file has been added here.

enter image description here

None of the posted solutions worked for me. So, hopefully this post helps someone out there. My app only crashes in release builds btw. Lesson learned. Always test release builds!

BTW, I found this SO post the most helpful https://stackoverflow.com/a/6708837/951349 .

Community
  • 1
  • 1
SmileBot
  • 19,393
  • 7
  • 65
  • 62
6

i got same issue with @Dominic Williams

try to change the momd file name in below(you can find this in managedObjectModel method in default), which as same as the [file name].xcdatamodeld file you created:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"[same with name of xcdatamodeld]" withExtension:@"momd"];
Alex Muller
  • 1,565
  • 4
  • 23
  • 42
rock
  • 179
  • 1
  • 2
  • 10
5

I had the same problem. The solution was a mix of 2 answers:

1) I had to add the "subdirectory" parameter into the URLForResource call

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DATAMODEL_NAME" withExtension:@"mom" subdirectory:@"DATAMODEL_NAME.momd"];

2) For a reason I don't know, the data model was not included when the project was compiled. I had to add it manually into the "Build Phases / Compile resources".

With only one of the above solutions, my application didn't work.

kgdesouz
  • 1,966
  • 3
  • 16
  • 21
4

The solution to the problem you speak of is simple. Change the file extension to "mom" instead of "momd" in the model URL. Done.

pfuri
  • 464
  • 1
  • 4
  • 10
4

I tried all the solutions here, and none of them worked. My issue appeared after I renamed the project. Apparently, Xcode continues to look for the old momd file in the wrong place during compilation.

For anyone that tried all the above solutions without success, try checking the full path of your .xcdatamodeld file. That's what worked for me.

XCool
  • 976
  • 11
  • 32
4

I had the same problem, it worked fine on iOS6 but not on iOS5. This is how I solved it:

  1. Create a new Model version in xcode. (select the .xcdatamodeld, open the Editor menu and click "Add Model Version...")
  2. Compile and make sure that the new version works.
  3. Set the old one as current version. ("Current" in File Inspector for the .xcdatamodeld under Versioned Core Data Model)
  4. Remove reference to the .xcdatamodeld file in xcode
  5. Right click the .xcdatamodeld file in Finder and choose "Show Package Contents"
  6. Delete the new .xcdatamodel that you dont want
  7. Re-add the .xcdatamodeld in xcode
  8. Compile and smile

(This is where I found how to delete a Model version: How to delete an old/unused Data Model Version in Xcode)

Community
  • 1
  • 1
fredrik
  • 13,282
  • 4
  • 35
  • 52
4

I solve the problem without changing any code.

I add the ModelName.xcdatamodeld through File->Add File instead of dragging the file into the Xcode.

NSString *path=@"ModelName";

NSURL *modelURL = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"momd"]];

model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
Chris So
  • 711
  • 1
  • 11
  • 23
1

I know this doesn't solve your problem but I ran into this problem yesterday that plagued me for hours, the solution @Dominic Williams posted gave me a ArrayIndexOutOfBoundsException (whatever the Objective-C equivalent is).

I'm not very good with Objective-C/Xcode yet but I'm working on an iOS app that our company had (mostly) developed externally. Unfortunately, they would often forget how to use a keyboard and would use capital letters interchangeably or spell properties incorrectly but were too lazy to go back and change it. They had used a capital letter in the xcode project name where it was not supposed to be (our product name doesn't use a capital) and I had to go back and change every occurence of this upper-case letter to a lower-case; which included the project name, core data file, hundreds of variables, etc.

Anyways, once I had done this I ran into this error and no solution was fixing it for me. I had made sure that all URL names were correct, project cleaned, app uninstalled, phone restarted, etc with no avail. I gave up and turned off my Mac and went home for the day. To my surprise, I came back in this morning and everything seemed to work fine!

I have no idea why this worked but if your stuck, try restarting your Mac.

javajavajavajavajava
  • 1,223
  • 2
  • 12
  • 20
1

If someone is stuck because of the same issue. Make sure you have properly linked the database (as you might have copied the code directly from some example).
Just update the name of the database in managedObjectModel and persistentStoreCoordinator methods in AppDelegate.

nomann
  • 2,257
  • 2
  • 21
  • 24
1

I had the same issue, i.e.

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyModel" withExtension:@"momd"];

returned nil, because there was no .momd file generated.

The reason was that in the app directory (e.g. MyGreatApp/MyGreatApp.app) xcode had copied the MyModel.xcdatamodeld instead of generating the MyModel.momd from the MyModel.xcdatamodeld file (using momc).

The solution was to delete the reference to MyModel.xcdatamodeld inside the XCode project browser and drag it back into the project from the finder. After that xcode realized it needed to compile it to a .momd.

Trausti Kristjansson
  • 2,974
  • 2
  • 16
  • 11
1

I had this problem out of nowhere after I deleted the generated app in the ~/Library/Application Support/iPhone Simulator. Somehow this caused subsequent builds to fail in the simulator and on devices. Hadn't changed anything to do with CoreData for ages but it would fail with the Cannot create an NSPersistentStoreCoordinator with a nil model . Tried a few things above and nothing worked.

I could see the generated momd folder with the mom file inside. The app in the simulator could see both but it failed to generate the sqlite file.

What solved it was adding an attribute to an entity in my xcdatamodeld file in Xcode and then immediately deleting it. I was hoping this would get Xcode to regenerate whatever was causing the issue from scratch and it seemed to work. Still not clear what was actually wrong, but my app is running in the simulator and devices again now.

Alistair McMillan
  • 1,160
  • 10
  • 23
1

On my side the problem was that I'd changed the case of a couple of characters in the database name.

When starting a new project, Xcode automatically sets everything up from the project name. If you started out calling your project "Rugbyontv" and later decided to change to "RugbyOnTV" and did a search and replace, that would break it. (Credit to Rob for pointing out that the name is case-sensitive)

1

I have been looking an answer for hours and nothing worked. But then I suddenly found this article. So according to this, the problem was in setting root controller in this part of AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    ListViewController *rootView = (ListViewController *)self.window.rootViewController;
    rootView.managedObjectContext = self.managedObjectContext;
    return YES;
}

In fact you should define what root controller will delegate your CoreData connection. And in my case I had a TabBarController connected to other views, so my targed view root controller was defined as TabBar and it caused an error. I changed

ListViewController *rootView = (ListViewController *)self.window.rootViewController;

to

ListViewController *rootView = (ListViewController *)self.window.superview;

and everything worked.

Costique
  • 23,712
  • 4
  • 76
  • 79
kokoko
  • 2,705
  • 3
  • 17
  • 28
0

I just had a similar problem upgrading from IOS5 to IOS6. Turns out it was a case sensitive issue with the model name.

Not sure if this helps anyone.

Troy
  • 961
  • 6
  • 13
  • 24
0

See also this thread: Unit Test can't find Core Data model file

It helped my solving the issue - which occurred only with Unit-Testing

Community
  • 1
  • 1
Thorsten Niehues
  • 13,712
  • 22
  • 78
  • 113
0

Okay, I'll tell a half-solution first, it will work if you re-install the app(in simulator or debugger device). But that's not a real solution for sure. For example, if you are updating your application, do NOT do that or your new version may crash because users won't re-install it, they'll use update button instead.

As I understood, this problem happens mostly when you change the data model file name. The reason might be like this:
. When you run the app for the first time, it is creating a data model file in app bundle like "data_model_1". This creation happens only for first time.
. When you update the file name and run the app again, it won't be able to find because there is still "data_model_1" file but you are telling it to look for "data_model_2". How can it find, it has not created it yet and will not unless you don't install the app with new file name.

So, if this is the first version of your app and re-installing on simulator or device doesn't harm your project, go for it. Else, check Core Data Model Versioning and Data Migration Guide on iOS Developer Library, maybe it's what you need.

Edit: If reinstalling does not work, try uninstalling first, then cleaning project, then close everything, reopen project and build+run. That should work.

kubilay
  • 5,047
  • 7
  • 48
  • 66
0

If your project works on the simulator but not on the device, try running the release-build on your device instead of the debug-build.

Select your project -> Product -> Edit Scheme -> Build Configuration [DEBUG -> RELEASE]

Now run the project again, it will work.

stackr
  • 2,742
  • 27
  • 44
0

I am also facing this problem but when i change the ModelName.xcdatamodeld file it is working. so i think ModelName.xcdatamodeld file not added to properly, so once check and clean the app and run it.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
0

After I fixed the naming issue, the error remained. Then it worked after restarting Xcode 5. This might automatically do the same thing as some of the manual linking suggestions offered here.

EthanP
  • 1,663
  • 3
  • 22
  • 27
0

For me the problem was due to the fact that I copy pasted my model from a sandbox project to my actual project. Always make sure to generate your model from within the project that the model is being used.

FujiRoyale
  • 762
  • 10
  • 26
0

I faced this same error when i renamed the .xcdatamodel file from xcode and changed the renamed name in app delegate wherever it was needed, but i still got the same error. None of the suggested procedure worked for me.

Then I opened the folder in Finder and found an additional file .xccurrentversion along with .xcdatamodel file. I opened it up in TextEdit app and changed this:

<dict>
    <key>_XCCurrentVersionName</key>
    <string>Your_Renamed_Model_FileName.xcdatamodel</string>
</dict>

I am using this with Xcode 6.4, OSX Yosemite 10.10.1

I hope it helps!

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56