这是viewDidLoad中fetchRequest的代码,代码是从找到here的教程中得到的,只是我以编程方式链接导航控制器和tableview而不是使用界面构建器。实体ProductInfo存在。但是,当我运行程序时,我收到错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'ProductInfo''
我已经重置模拟器,因为它是一个旧模型,但错误仍然发生。我也切换到使用FetchedResultsController,但问题仍然存在。问题是因为这些fetchedResultsController方法不在appdelegate中?它们目前位于TableViewController中。我该如何解决这个问题?
viewDidLoad方法:
- (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;
}
任何帮助都非常感谢。谢谢。
如果我粘贴的上述片段没有帮助,我也将整个项目与数据模型结合在一起。
答案 0 :(得分:5)
每次交换机开发计算机时都会发生这种情况。我要做的是
答案 1 :(得分:3)
之前遇到过这个问题,听起来好像是xcdatamodeld文件中实体名称的简单拼写错误。字符串“ProductInfo”必须与模型文件中的实体名称完全匹配。
也可能是您的上下文没有正确引用。如果上述问题无法解决问题,请考虑显示与上下文相关的更多代码。