我正在创建一个基于视图的核心数据应用程序。我可以创建managedObjectContext并在app委托中“使用”它,但不能将它传递给mainviewcontroller。可能是简单的事情,但在找了一段时间后我找不到问题。 managedviewModel在mainviewcontroller中为nil。
日志和错误在这里:
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)
app appate代码:
-(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];
}
感谢。
答案 0 :(得分:0)
如果您确定已添加该实体(Shelf),请从模拟器或设备中删除该应用,然后尝试执行干净的构建,然后重新构建。它可能会保留旧版本的模型。
我的另一个想法是你如何在MainViewController(.h)中声明NSManagedObjectContext。你保留它吗?
答案 1 :(得分:0)
我不知道问题是什么,但我用同样的方式创建了一个新的视图控制器,现在一切正常。
感谢。
比尔