3

I'd like to thank all the people that contribute to this website. I am one h*ll of a noob at programming and you all help me out so much. With that said, let's get on with my latest problem!

I am creating an app that uses a UIManagedDocument to store a Core Data model for me. I use to create the UIManagedDocument with the following code, but now with Xcode 4.3.2 (The one that updates with the Mac App Store) I have an issue.

Here's the code:

    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"MyFile"];

    NSLog(@"\n\n This line does print. \n\n");
    UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:url];
    NSLog(@"\n\n This line does not print \n\n");

I receive an error in the console that goes like this:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't merge models with two different entities named 'Foo''

Here's the thing, I'm not trying to get versions or anything like that to work. I'm simply creating a data model with two types of entities, and trying to get a UIManagedDocument to open in my app: super simple, right?

I have uninstalled Xcode and reinstalled it to no luck. I have also deleted my Core Data Model and redone it from scratch with no luck.

I am completely lost and have already spent about 2 nights trying to troubleshoot this.

Help me! Please! Thanks for reading. If you need anymore information let me know. I'll give you my soul for an answer...

Caborca87
  • 187
  • 1
  • 16
  • Any chance you have more than one .xcdatamodel being added to your bundle? Or maybe multiple versions of the model with the same version number? – paulmelnikow May 11 '12 at 05:46
  • I only have one .xcdatamodel in my project folder. And I literally create the new Core Data Model, add two entities, then run the project. What could I be doing to create multiple versions of the same Data Model? – Caborca87 May 11 '12 at 06:00
  • If you don't know, you're probably not doing it (there's an "Add Model Version" option in Xcode). – paulmelnikow May 11 '12 at 06:02
  • I know that the error is happening in the initWithFileURL method of the UIDocument class. Beyond that, I'm completely stuck. I have noticed that the .xcdatamodel file is being converted to a .xcdatamodeld file (notice the appended 'd'), by the way. Does that tell us anything? – Caborca87 May 11 '12 at 06:05
  • That's normal in my experience. You could try subclassing UIManagedDocument and overriding `-managedObjectModel`. I do it like this: `[NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle bundleForClass:[self class]]]]` but if that still duplicates the entity, maybe some different code will work. Honestly I don't know what's causing the problem you're seeing; I'm just suggesting what I would try. – paulmelnikow May 11 '12 at 06:11
  • I think I should be able to do that without too many problems. It's just frustrating, because I only have one model and the documentation specifically states that **You do this if, for example, your application supports multiple document types, each of which uses a different model. You want to ensure that the models are not merged for each document class.** I shouldn't be getting this error since I have only one model. – Caborca87 May 11 '12 at 14:31
  • I'll post sometime next week with my solution, if I find one. Thanks for your help @noa – Caborca87 May 11 '12 at 17:40
  • So I figured it out (or at least I'm pretty sure I did). I didn't import the Core Data framework into the helper class that handles all of this... I'll post an answer to this next week. WOOPS – Caborca87 May 12 '12 at 02:38

1 Answers1

1

So I figured it out.

I had a hard time believing that I had to subclass UIManagedDocument. Ultimately I didn't' have to. I had some problems with the way my files were linked to the project.

I followed the steps here and it's all good now

core data application is Crashing in iphone device

Community
  • 1
  • 1
Caborca87
  • 187
  • 1
  • 16