Iphone 核心数据无法将managedObjectContext从应用程序委托传递到视图控制器
我正在制作一个基于视图的核心数据应用程序。我可以创建managedObjectContext并在应用程序委托中“使用”它,但无法将其传递给mainviewcontroller。可能是一些简单的问题,但我找了很长一段时间后还是找不到问题。mainviewcontroller中的managedObjectModel为零 日志和错误如下所示:Iphone 核心数据无法将managedObjectContext从应用程序委托传递到视图控制器,iphone,Iphone,我正在制作一个基于视图的核心数据应用程序。我可以创建managedObjectContext并在应用程序委托中“使用”它,但无法将其传递给mainviewcontroller。可能是一些简单的问题,但我找了很长一段时间后还是找不到问题。mainviewcontroller中的managedObjectModel为零 日志和错误如下所示: 2010-06-02 11:01:10.504 TestCoreData[404:207] Could not make MOC in MainViewCont
2010-06-02 11:01:10.504 TestCoreData[404:207] Could not make MOC in MainViewController implementation.
2010-06-02 11:01:10.505 TestCoreData[404:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Shelf''
2010-06-02 11:01:10.506 TestCoreData[404:207] Stack: (
30864475,
2452296969,
28852395,
12038,
3217218,
10258,
2700679,
2738614,
2726708,
2709119,
2736225,
38960473,
30649216,
30645320,
2702869,
2740143,
9704,
9558
gdb
此处的应用程序委派代码:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
self.mainViewController = aController;
[aController release];
NSLog(@"before did finish launching");
if (self.managedObjectContext == nil) {
NSLog(@"Could not make MOC in TestCoreDataAppDelegate implementation.");
}
//Just to see if I can access the here.
NSFetchRequest* request = [[NSFetchRequest alloc] init];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Shelf" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
if (!entity) {
NSLog(@"No Entity in TestCoreDataAppDelegate didfinishlaunching");
}
NSLog(@"passed did finish launching");
NSManagedObjectContext *context = [self managedObjectContext];
self.mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
self.mainViewController.managedObjectContext = context;
[context release];
[window addSubview:[mainViewController view]];
[window makeKeyAndVisible];
return YES;
}
此处MainViewController中的代码:
@implementation MainViewController
@synthesize managedObjectContext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
if (self.managedObjectContext == nil) {
NSLog(@"Could not make MOC in MainViewController implementation.");
}
NSFetchRequest* request = [[NSFetchRequest alloc] init];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Shelf" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
}
谢谢。如果您确定已添加实体工具架,请从模拟器或设备中删除该应用程序,并尝试进行干净的构建,然后重新构建。它可能会保留旧版本的模型
我的另一个想法是如何在MainViewController.h中声明NSManagedObjectContext。您是否保留了它?我不知道问题出在哪里,但我以我认为相同的方式制作了一个新的视图控制器,现在一切正常 谢谢
比尔Matt,我清空了所有的储藏室,清理了所有的目标。我把nsfetchrequest放在app委托中的想法是测试模型本身,因为它不会导致任何错误,所以我认为数据模型是可以的。以下是MainViewController.h中的声明:@property非原子,retain NSManagedObjectContext*managedObjectContext;因此它被保留了下来。我会调查的,但现在说实话,我甚至不知道是否应该。应该吗?