Adding core data to project without autogenerate

Problem: Implementing core data to your project after you created the project without “use Core Data for storage”

Description: I’m new to iOS development and therefor I’m new to core data. So, I’ve created my project without the option “use Core Data for storage”. After few days of development I realised that NSUserDefaults just ain’t gonna cut it. I needed a database.

My first step was to Google “ios core data example” and get right to it. But soon I got bitch slapped by compiler and after that debugger took a swing too. My next move was a wee bit smarter 🙂 I took a step back and that leads us to our next chapter…

Solution:

I’ve decided to create a new project with “use Core Data for storage” options. I followed a step by step tutorial: http://developer.apple.com/library/ios/#DOCUMENTATION/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html

Tutorial is really nice and explains things in detail so I will not do the same thing. After implementing project explained in tutorial i ran my program and everything worked just fine. I’ve copy pasted stuff from my project with “use Core Data for storage” options to my old project and things just where not working again. I check all hard-coded strings twice and everything look OK.

At this point I was really stuck so I asked a help from experienced iOS developer. BTW: Tnx Chupa for bestowing me with a new-found knowledge 🙂

So the deal breaker was in managedObjectModel:

- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel_ != nil) {
        return managedObjectModel_;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"PrototypeMoneyMaker" withExtension:@"momd"];
    managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return managedObjectModel_;
}

When you create a project with “use Core Data for storage” option xcode generates model with momd extension. But when you create it yourself extension is mom. I’m not really sure why is that. So to keep things short. Your method should look like this:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"PrototypeMoneyMaker" withExtension:@"mom"];

After I changed that everything worked fine. If it still doesn’t work for you try to check all hard-coded strings and in general compare your project that was created with “use Core Data for storage”.

Hope this helps. If anyone can explain this mysterious mom & momd extension I would be a happy little camper. All comments are welcomed. I publish all my blog notifications on my twitter so if you wanna you can follow me @ http://twitter.com/PavlovicMario

About buildingmyworld

Will fill this part later. Captain procrastinator mode is currently on!
This entry was posted in iOS Development, xcode and tagged , , , , , , , . Bookmark the permalink.

1 Response to Adding core data to project without autogenerate

  1. Chris says:

    The difference between .mom and .momd is one doesn’t have model versioning and one does, respectively. In Xcode 4, you control this by:
    1. selecting your .xcdatamodeld
    2. accessing the File inspector (in the Utilities panel on the right-side of Xcode)
    3. setting “Target Membership” to include your main Target

    This one drove me crazy for hours, but your post put my on the right path.

    Thanks!

Leave a comment