views:

239

answers:

2

Hi,
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

+1  A: 

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  
}
unforgiven
The MOC itself is not nill, but that got me thinking about the ordering on my ViewControllers. I was actually trying to pass the MOC through to a sub-ViewController and therefore it's the second view controller itself which was nil at this point.Therefore I changed things around so that my sub-view retrieves the MOC from it's parentView in the viewDidLoad method i.e. self.managedObjectContext = parentViewController.managedObjectContext; Thanks for the help resolving this, very much appreciated.
Paul
In order to avoid this kind of problems, I always retrieve the managedObjectContext directly from the app delegate, which is always available in whatever view controller I instantiate.
unforgiven
That's a good idea, it sounds much safer. In future I'll make sure all my ViewControllers have a reference to the AppDelegate and then I'll retrieve it directly in the viewDidLoad method. Thanks again.
Paul
A: 

According to the last entry by Paul, I would like to know how to connect in the interface builder the declared managedObjectContext in the app delegate for the sub-view controller.

Joe