2

The is the code for the fetchRequest in viewDidLoad and the code is followed from a tutorial found here just that I'm linking the navigation controller and the tableview programmatically instead of using interface builder. The entity ProductInfo exists. However when I run the program I get the error:

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

I have reset the simulator incase it was an old model but the error still occurs. I have also switched to use a FetchedResultsController but the problem still persists. Is the problem because these fetchedResultsController methods aren't inside the appdelegate? They are currently in a TableViewController. How can I solve this problem?

viewDidLoad Method:

- (void)viewDidLoad{

NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription entityForName:@"ProductInfo" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError * error;
self.productInfos = [_context executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
[super viewDidLoad];}

ProductInfo.h:

@class ProductDetails;

@interface ProductInfo : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * productName;
@property (nonatomic, retain) NSString * productPrice;
@property (nonatomic, retain) ProductDetails * details;

@end

FetchedResultsController

-(NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest  * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity  = [NSEntityDescription entityForName:@"ProductInfo" inManagedObjectContext:_context]; //line that is causing the problem
[fetchRequest setEntity:entity];

NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"productInfos.productName" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController * theFetchedResultsController  = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext: _context sectionNameKeyPath:nil cacheName:@"Root"]; 
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = nil;

[sort release];
[fetchRequest release];
[theFetchedResultsController release];

return _fetchedResultsController;

}

Any help much appreciated.Thanks in advance.

In case the above fragments i pasted did not help, I attached the whole project with the data model inside too.

http://www.mediafire.com/?5cns4q0sv9hqn6s

Hong Yi
  • 569
  • 2
  • 13
  • 29

2 Answers2

5

This happens to me every time a switch development computers. What I have to do is

  1. Uninstall the app from the simulator.
  2. Rename the database file that stores the UIManagedDocument.
  3. Perform a Build Clean.
  4. Build and run the application.
dasdom
  • 13,975
  • 2
  • 47
  • 58
Henrik
  • 1,078
  • 1
  • 9
  • 17
  • Thanks, this helped me. However, I skipped step 2 and in my case it worked fine. Also, it doesn't happen every time one switches development computers (I do that all the time). I think it only happens when you make a modification to the core data model and then switch computers. – shim Sep 25 '12 at 03:43
3

Having run into this problem before, it sounds like it might be a simple misspelling of the entity name in the xcdatamodeld file. The string "ProductInfo" must match exactly the name of the entity in the model file.

It might also be that your context is not making the correct reference. Consider showing some more of your code related to the context if the above doesn't fix the issue.

Jacob M. Barnard
  • 1,347
  • 1
  • 10
  • 24
  • 1
    I took a look at the project, at the time viewDidLoad gets called where the NSEntityDescription is created, the context of ProductViewController is nil. So your second guess was right. – Martin Brugger May 18 '11 at 04:28
  • so what should i do to rectify that? i'm still new to core data. – Hong Yi May 18 '11 at 05:39
  • 1
    It depends on the design pattern of your app, but I normally go with a custom singleton DataCenter class that houses the NSManagedObjectContext, the NSFetchedResultsController, and a hierarchy of model objects representing entities in the data model. These are initialized in the app delegate for you if you picked the correct template in Xcode. It's just a matter of migrating them. If you used a different template, look at the Xcode template CoreData setup to get an idea. – Jacob M. Barnard May 18 '11 at 05:47
  • i have switched to use a NSFetchedResults controller but the error still doesn't go away. Would you mind explaining to me what does it mean when my context is nil? And what should I do to prevent this from happening in the future? – Hong Yi May 18 '11 at 06:43
  • If your context is nil then you do not have a connection to your xcdatamodeld file. So, either you are releasing it before use or you are never setting it. My previous comment describes a solution: use a single object constructed by one class (singleton) to house references to all your persistent data and persistent connections to data sources. You could create a utility to access the singleton after it is initialized as a property of the app delegate, or you could make it a true [link](http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like)singleton. – Jacob M. Barnard May 18 '11 at 13:13