简体   繁体   English

核心数据无法将ManagedObjectContext从应用程序委托传递给视图控制器

[英]Core-Data Can't pass managedObjectContext from app delegate to view controller

I'm making a core-data application that is view based. 我正在制作一个基于视图的核心数据应用程序。 I can create the managedObjectContext and 'use' it in the app delegate, but can not pass it to the mainviewcontroller. 我可以创建ManagedObjectContext并在应用程序委托中“使用”它,但是不能将其传递给mainviewcontroller。 Probably something simple, but I can't find the problem after looking for quite a while. 可能很简单,但经过一段时间后我找不到问题。 The managedObjectModel is nil in the mainviewcontroller. managedObjectModel在mainviewcontroller中为nil。

The log and error is here: 日志和错误在这里:

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) )(gdb)

Code for app delegate here: 此处的应用程序委托代码:

-(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;
}

Code in MainViewController here: 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];
}

Thanks. 谢谢。

If you're sure you've added that entity (Shelf), remove the app from the simulator or device and try to do a clean build and then re-build. 如果确定已添加该实体(扩展架),请从模拟器或设备中删除该应用,然后尝试进行全新构建,然后重新构建。 It may be holding on to an old version of the model. 它可能会保留旧版本的模型。

Another thought I had is how are you declaring the NSManagedObjectContext in your MainViewController (.h). 我的另一个想法是如何在MainViewController(.h)中声明NSManagedObjectContext。 Are you retaining it? 你保留吗?

I don't know what the problem was, but I made a new view controller in what I thought was the same way, and now everything works. 我不知道问题出在哪里,但是我以我认为相同的方式制作了一个新的视图控制器,现在一切正常。

Thanks. 谢谢。

Bill 法案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM