Core-Data iPhone:找不到NSManagedObjectModel [英] Core-Data iPhone: could not locate an NSManagedObjectModel

查看:118
本文介绍了Core-Data iPhone:找不到NSManagedObjectModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Apple's CoreDataBooks sample project as a learning aid for core data.

我修改了应用程序,以便在加载应用程序时首先显示菜单页面,而不是Books表格视图(RootViewController)。

I modified the app so that when the app loads I show a menu page first - not the Books tableview (RootViewController).

我完成了以下操作:

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

I created a menu page in interface builder (just a view with a button on it)

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

The CoreDataBooksAppDelegate.h now looks like this:

// 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看起来像这样:

The CoreDataBooksAppDelegate.m looks like this:

#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的其余部分保持不变。

The rest of CoreDataAppDelegete.m remains unchanged.

在MenuViewController中,当按钮被点击时,发生以下动作:

In the MenuViewController when the button is clicked, the following action takes place:

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

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

In IB I changed the MainWindow.xib to call the MenuViewController rather than the RootViewController.

所以,应用程序加载,菜单正确显示按钮。点击按钮,应用程序在RootViewController的viewDidLoad内部崩溃。

So, the app loads and the menu is displayed properly with the button. Upon clicking the button the application crashes inside of the RootViewController's 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");
}



在控制台中我收到以下信息:

In the console I receive the following:

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

我已经阅读了这个异常,但我不知道正确的步骤来解决它。

I have read about this exception but I do not know the proper steps to resolve it.

任何帮助将不胜感激。

Any help would be appreciated.

感谢您的时间,
Vivas

Thank-you for your time, Vivas

推荐答案

Ok。

将以下代码放在RootViewController的viewDidLoad中消除了错误:

Placing the following code inside of the RootViewController's viewDidLoad eliminates the error:

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

我发现在SO上有类似问题的人:链接文本

I found someone with a similar problem here on SO: link text

由于Aryeh在这篇文章中指出:简而言之,你试图从一个尚未设置的objectContext中获取一个实体,因此你的选项是在该视图加载之前设置它或者在应用中的其他地方。

As Aryeh pointed out in that post: "In short you are trying to fetch an entity from an objectContext that hadn't been set up yet. Your options therefore are to set it up right then or do elsewhere in the app before this view loads."

这篇关于Core-Data iPhone:找不到NSManagedObjectModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆