Could not locate NSManagedObjectModel for Entity

。_饼干妹妹 提交于 2019-12-24 00:30:21

问题


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


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/6039660/could-not-locate-nsmanagedobjectmodel-for-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!