无法找到实体的 NSManagedObjectModel

发布于 2024-11-08 02:46:06 字数 2594 浏览 0 评论 0原文

这是 viewDidLoad 中 fetchRequest 的代码,该代码来自找到的教程 这里只是我以编程方式链接导航控制器和表格视图,而不是使用界面生成器。实体 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;

}

非常感谢任何帮助。提前致谢。

如果我粘贴的上述片段没有帮助,我也附加了整个项目以及内部的数据模型。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倦话 2024-11-15 02:46:06

我每次切换开发计算机时都会发生这种情况。我要做的就是

  1. 从模拟器中卸载该应用程序。
  2. 重命名存储 UIManagedDocument 的数据库文件。
  3. 执行构建清理。
  4. 构建并运行应用程序。

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.
紅太極 2024-11-15 02:46:06

之前遇到过这个问题,听起来可能是 xcdatamodeld 文件中实体名称的简单拼写错误。字符串“ProductInfo”必须与模型文件中实体的名称完全匹配。

也可能是您的上下文没有提供正确的参考。如果上述方法不能解决问题,请考虑显示更多与上下文相关的代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文