0

I created a single view application that was running fine, but when I add core data in it just by copying the same code that Xcode generates in Empty Project using Core Data.

But my application crashes by saying that It is unable to create the Persistent Store Coordinator. Applying breakpoints on all exceptions I came to know that my application is crashing on this line

 _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

Before this, I have verified the URL in Managed Object Model. URL is returning NULL.

- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"ProjectName" withExtension:@"momd"];

    NSLog(@"Url:%@",modelURL);
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

I have done all the necessary things to add core data in single view application but using this link Add Core data in Single View Application

Personal perception :: I think something going wrong with URL, as it is returning NULL.

Please Please Help me, I am in great trouble.

Thanks in anticipation

iOmi
  • 625
  • 10
  • 24
  • Docs for `URLForResource` say that it returns nil if there's no matching file? If you look inside your app bundle, is there a "ProjectName.momd"? – Phillip Mills Nov 12 '12 at 12:28
  • @PhillipMills :: Thanks for answering as early, I have checked but there is no "ProjectName.momd" file in my app bundle. Does it means that my application is not creating the Core DB file? If is it so, what to do? – iOmi Nov 12 '12 at 12:34
  • The "momd" file is the folder that contains the actual model that you design with your entities and relationships. Perhaps you've given your model a different name or perhaps you haven't included it in the compile phase for your project. – Phillip Mills Nov 12 '12 at 12:39
  • If searched it but I couldn't find anything like "momd" or "ProjectName.momd" in my application bundle. My model name is "Model". If you please have look on this link " [link](http://remapps.net/2012/02/07/adding-core-data-to-a-project/) , I have performed all the steps but no "momd" or "project name.momd" is created in my app bundle. – iOmi Nov 12 '12 at 12:49
  • My entity name is "Data", only the first alphabet is capital, not all. Does it make any issue? – iOmi Nov 12 '12 at 12:53
  • The content of the model isn't going to affect whether or not the file exists (unless, perhaps, there's some compile error being reported). I suspect that when you added the model file, you may not have told Xcode to include it in your target. Look at the Compile Sources section of your target's Build Phases and make sure your model file is in that list. – Phillip Mills Nov 12 '12 at 13:06

1 Answers1

1

I believe it is your URLForResource:@"ProjectName" that is causing the issue. Try replacing that block of code with:

- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
    return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];

NSLog(@"Url:%@",modelURL);
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}

Unless you renamed your .xcdatamodeld file when you created your Core Data model, this will work. To check what URLForResource: should point to, check the name of your .xcdatamodeld in your bundle. In order for your current code to work, it would have to be called ProjectName.xcdatamodeld.

Andrew Robinson
  • 3,404
  • 3
  • 20
  • 26
  • Thanks for you answer Andrew Robinson, is it necessary that the name of project and our .xcdatamodeld file should be same? If they are not same then what happened? – iOmi Nov 13 '12 at 04:28
  • My URL for Resource is still returning Null. This is the total issue of this URL that is creating problem in making persistent store coordinator. I tried your answer but not worked for me. Do have any other trick? – iOmi Nov 13 '12 at 04:41
  • Your Project and Data Model do not need to have the same name, you can actually name it anything you want. If the code above is not working, verify what your Data Model is named. If your model is a versioned Data Model and is named what you put in URLForResource: it should initialize so long as the rest of your code is correct. Note that the URLForResource: value is case sensitive, you need to type it exactly as your model is named. – Andrew Robinson Nov 13 '12 at 13:30