开发者

Multiple Unrelated Entities in one Core Data Model

开发者 https://www.devze.com 2022-12-20 02:43 出处:网络
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.

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 entit开发者_运维知识库y 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


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  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消