Paul
Paul

Reputation: 79

Multiple Unrelated Entities in one Core Data Model

I want to have the equivalent of two completely unrelated tables in my database, and for this I have defined two different entities in my Core Data model.

Further to this, I have a different ViewController making use of each of these entities and therefore I'm initializing two different fetchedResultsController (one in each ViewController) in this manner:

// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" 
    inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

I should note that the MOM is only initialized only once in the AppDelegate based on the XCode template.

It all worked fine when I only had one ViewController and a single entity in the model, however even though I have the 2nd entity defined in the model I cannot get the 2nd ViewController to initialize the fetchedResultsController (again based on the XCode template). I always get the following error:

2010-02-11 22:02:55.078 JournalTool[3094:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'EventTag''

I'm probably missing something basic and doing something really stupid, but any help here would be appreciated.

Thanks,
Paul

Upvotes: 1

Views: 1155

Answers (1)

Massimo Cafaro
Massimo Cafaro

Reputation: 25419

It may be that the managedObjectContext you pass to your second view controller is actually nil. Before calling

[tagsViewController setManagedObjectContext:self.managedObjectContext];

verify that self.managedObjectContext is not nil:

if(!self.managedObjectContext){
  NSLog(@"invalid managedObjectContext");
  // now get a valid managedObjectContext
  // and pass it to your view controller  
}

Upvotes: 1

Related Questions