我正在製作基於視圖的核心數據應用程序。我可以創建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];
}
謝謝。
馬特, 我清空緩存並清理所有目標。我將nsfetchrequest放在應用程序委託中的想法是測試模型本身,因爲它不會導致任何錯誤,我認爲數據模型是可以的。 這是MainViewController.h中的聲明: @property(nonatomic,retain)NSManagedObjectContext * managedObjectContext; 所以它被保留。我會研究它,但現在說實話,我甚至不知道它應該是什麼。應該是? – user346330 2010-06-02 18:29:38