2010-06-02 49 views
0

我正在制作基于视图的核心数据应用程序。我可以创建managedObjectContext并在应用程序委托中“使用”它,但不能将它传递给mainviewcontroller。可能有些简单,但在找了一段时间后我找不到问题。 managedViewModel在mainviewcontroller中为零。核心数据无法将managedObjectContext从应用程序委托传递给视图控制器

的日志和错误是在这里:

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

谢谢。

回答

0

如果您确定已经添加了该实体(Shelf),请从模拟器或设备中删除该应用程序,然后尝试执行干净构建并重新构建。它可能会保留模型的旧版本。

我的另一个想法是你如何在你的MainViewController(.h)中声明NSManagedObjectContext。你保留它吗?

+0

马特, 我清空缓存并清理所有目标。我将nsfetchrequest放在应用程序委托中的想法是测试模型本身,因为它不会导致任何错误,我认为数据模型是可以的。 这是MainViewController.h中的声明: @property(nonatomic,retain)NSManagedObjectContext * managedObjectContext; 所以它被保留。我会研究它,但现在说实话,我甚至不知道它应该是什么。应该是? – user346330 2010-06-02 18:29:38

0

我不知道是什么问题,但我做了一个新的视图控制器,我认为是相同的方式,现在一切正常。

谢谢。

比尔

相关问题