Core-Data iPhone:找不到NSManagedObjectModel

我正在使用Apple的CoreDataBooks示例项目作为核心数据的学习辅助工具。

我修改了应用程序,以便应用程序加载时,我首先显示一个菜单页面 – 而不是书籍tableview(RootViewController)。

我做了以下几件事:

我在界面生成器中创build了一个菜单页面(只是一个带有button的视图)

CoreDataBooksAppDelegate.h现在看起来像这样:

// for the menu @class MenuViewController; @interface CoreDataBooksAppDelegate : NSObject <UIApplicationDelegate> { NSManagedObjectModel *managedObjectModel; NSManagedObjectContext *managedObjectContext; NSPersistentStoreCoordinator *persistentStoreCoordinator; UIWindow *window; UINavigationController *navigationController; //for the menu MenuViewController *viewController; } - (IBAction)saveAction:sender; //for the menu @property (nonatomic, retain) IBOutlet MenuViewController *viewController; @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; @property (nonatomic, readonly) NSString *applicationDocumentsDirectory; @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UINavigationController *navigationController; @end 

CoreDataBooksAppDelegate.m看起来像这样:

 #import "CoreDataBooksAppDelegate.h" #import "RootViewController.h" // for the menu #import "MenuViewController.h" @implementation CoreDataBooksAppDelegate @synthesize window; @synthesize navigationController; // for the menu @synthesize viewController; #pragma mark - #pragma mark Application lifecycle - (void)applicationDidFinishLaunching:(UIApplication *)application { RootViewController *rootViewController = (RootViewController *)[navigationController topViewController]; rootViewController.managedObjectContext = self.managedObjectContext; // for the menu [window addSubview:viewController.view]; // Configure and show the window [window makeKeyAndVisible]; } 

CoreDataAppDelegete.m的其余部分保持不变。

在MenuViewController中点击button时,会发生以下操作:

 RootViewController *modalViewController1 = [[[RootViewController alloc] initWithNibName:nil bundle:nil] autorelease]; [self presentModalViewController:modalViewController1 animated:YES]; 

在IB中,我更改了MainWindow.xib来调用MenuViewController而不是RootViewController。

所以,应用程序加载和菜单正确显示与button。 当点击button时,应用程序在RootViewController的viewDidLoad内部崩溃。

它在这里崩溃:

 - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"1 START viewDidLoad RootViewController"); self.title = @"Books"; // Set up the edit and add buttons. self.navigationItem.leftBarButtonItem = self.editButtonItem; NSLog(@"2 setup button viewDidLoad RootViewController"); // Configure the add button. UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBook)]; self.navigationItem.rightBarButtonItem = addButton; [addButton release]; NSLog(@"3 viewDidLoad RootViewController"); NSError *error; // HERE IS THE CRASH SITE if (![[self fetchedResultsController] performFetch:&error]) { NSLog(@"Does not reach this point in viewDidLoad RootViewController"); // Update to handle the error appropriately. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); // Fail } NSLog(@"END viewDidLoad RootViewController"); } 

在控制台中,我收到以下内容:

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

我已经阅读了这个例外,但我不知道解决这个问题的正确步骤。

任何帮助,将不胜感激。

感谢你的时间,维瓦斯

好。

将下面的代码放在RootViewController的viewDidLoad中可以消除这个错误:

 if (managedObjectContext == nil) { managedObjectContext = [(CoreDataBooksAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; NSLog(@"After managedObjectContext: %@", managedObjectContext); } 

我在SO: link文本中发现了一个类似的问题

正如Aryeh在文章中指出的那样:“总而言之,你正尝试从一个尚未设置的objectContext中获取一个实体,因此你的select是在这个视图加载之前设置它,或者在应用程序的其他地方“。

在applicationDidFinishLaunching中:您正在执行以下操作:

 rootViewController.managedObjectContext = self.managedObjectContext; 

但是我没有看到self.managedObjectContext设置的代码。 applicationDidFinishLaunching在相当早的时候被调用。 你确定你设置了托pipe对象的上下文吗?

你有没有改变核心数据模型? 当应用程序的核心数据模型发生改变而不改变底层的SQLite数据库时,这个错误是很常见的,所以存储的数据和模型是不同步的。

尝试从模拟器或testing设备中完全删除您的应用程序,然后重新安装并重试。

在我的开发过程中,我找不到后来添加的实体。 什么为我工作:(基本上是一个理智提示:-)

卸载应用程序每次更改数据模型!

数据模型由Core Data在安装之间caching,以确保完整性保持完整。 从模拟器/ iPhone上删除应用程序,以便能够testing您的更改。

PS:有谁知道如何自动做到这一点?