Hon*_* Yi 2 core-data objective-c nsmanagedobject ios
这是viewDidLoad中fetchRequest的代码,代码遵循这里的教程,只是我以编程方式链接导航控制器和tableview,而不是使用界面构建器.实体ProductInfo存在.但是,当我运行程序时,我收到错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'ProductInfo''
Run Code Online (Sandbox Code Playgroud)
我已经重置模拟器,因为它是一个旧模型,但错误仍然发生.我也切换到使用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];}
Run Code Online (Sandbox Code Playgroud)
ProductInfo.h:
@class ProductDetails;
@interface ProductInfo : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * productName;
@property (nonatomic, retain) NSString * productPrice;
@property (nonatomic, retain) ProductDetails * details;
@end
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
}
任何帮助非常感谢.谢谢你提前.
如果我粘贴的上述片段没有帮助,我也将整个项目与数据模型结合在一起.
每次交换机开发计算机时都会发生这种情况.我要做的是
| 归档时间: |
|
| 查看次数: |
5365 次 |
| 最近记录: |