2
votes

I have a CoreData database which works on the root view controller. I have a second UIViewController of which when I switch to, and use the same exact line to fetch NSEntityDescription, I get the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: +entityForName: could not locate an NSManagedObjectModel for entity name 'Channel''

I am doing that as follows:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Channel" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];

The error hits at the NSEntityDescription *entity line. How is this possible if the line works on another UIViewController?

Thank you

2

2 Answers

3
votes

The error is suggesting that the ManagedObjectContext have a problem.

Are you passing your ManagedObjectContext from your first view to the second (if not try it)

If this doesn't work can you post the code where you're assigning self.context


You mean code like this one in your appDelegate in the awakeFromNib method:

RootViewController *rootViewController = (RootViewController *)[self.navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;

You need the second line to pass the actual managedObjectContext

You can put that second line in your firstView like this:

//secondViewController creation here
secondViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:secondViewController
                                         animated:YES];

Of course you need to set up the necessary @property.
In doing so from UIViewController to UIViewController starting from the appDelegate you will be passing a reference to that instance of NSManagedObjectContext.


UITabBarController Class Reference is there if you need more information.
Just make sure that when you make the assignment the IBOutlet are set and that your NSManagedObject have been created.

An array of the root view controllers displayed by the tab bar interface.
@property(nonatomic, copy) NSArray *viewControllers

0
votes

What solved it for me was to load the NSEntityDescription manually instead of using the convenience method from NSFetchRequest:

NSFetchRequest *fetchRequest = [[NSFetchRequest new] autorelease];
fetchRequest.entity = [NSEntityDescription entityForName:entity inManagedObjectContext:[self managedObjectContext]];