Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find momd file: Core Data problems

Aw geez! I screwed something up!

I'm a Core Data noob, working on my first iOS app. After much Stack Overflowing I'm using this code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"momd"];
if (!path) {
    path = [[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"mom"];
}
NSAssert(path != nil, @"Unable to find Resource in main bundle");

CoreData is the name of my app.

I've tried to put in initial data into the app by finding the path to the sqlite file in my iPhone simulator, and then going and inserting into that sqlite file. But at some point, I moved the sqlite (thinking it would create a fresh copy), deleted the app from the simulator, and the sqlite file is gone. I'm not sure if I'm leaving out some part of the process (this was a few hours ago) but the end result is that everything is screwed up.

How do I resubstantiate this sqlite / momd file? "Clean" and "Clean all targets" are grayed out.

I'm happy to post the relevant code from my app that would help shed some light on this problem but there's tons of code relating to Core Data which I don't understand, so I'm not sure what part to post! Any help is greatly appreciated.

like image 393
thekevinscott Avatar asked Dec 27 '10 03:12

thekevinscott


1 Answers

Here are a few recommendations:

  • The code you posted to get the .mom(d) file is not exactly the recommended way. Use mergedModelFromBundles instead, as in

    self.managedObjectContent= [NSManagedObjectModel mergedModelFromBundles:nil];
    

    It takes care of getting the path, choosing/merging the correct mom or momd, and initializing of the MOC all by one step. You should use this. But note that you need to clean the build process once in a while, as discussed in this SO question/answer.

  • This shows that, although crawling through StackOverflow is often good, that's not the best approach when you deal with a big framework like CoreData.

    Honestly, take a day and read the documentation from the start to the end. You might want to google bits of code and to start coding immediately, but reading through the documentation definitely saves the development time considerably in the long run.

    Also, Marcus Zarra's CoreData book (see here) helped me a lot. It might look expensive, but it was totally worth while.

  • On a different topic, I don't think it a good strategy to put the pre-cooked sqlite file into the simulator's directory (inside ~/Library/Application Support/iPhone Simulator/) even for development purpose... because it doesn't work on the real device. If you need to do so, put the sqlite file as a resource of the app, and copy it at the launch time to either Documents or Caches, and use that afterwards.

like image 92
Yuji Avatar answered Nov 10 '22 18:11

Yuji