2010-03-22 105 views
2

我爲我的應用程序創建了一個ServerModel類,並且在其中我想連接到設備上的Sqlite數據庫,但是每當我去檢查我的NSManagedObjectContext時它都返回null。NSManagedObjectContext返回nil iphone

雖然我在其他類RootViewController的

在我的appdelegate同樣的工作以及我做

rootViewController.managedObjectContext = self.managedObjectContext; 

serverModel.managedObjectContext = self.managedObjectContext; 


#pragma mark - 
#pragma mark Core Data stack 

/** 
Returns the managed object context for the application. 
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the appalication. 
*/ 
- (NSManagedObjectContext *) managedObjectContext { 

    if (managedObjectContext != nil) { 
     return managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) { 
     managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [managedObjectContext setPersistentStoreCoordinator: coordinator]; 
    } 
    return managedObjectContext; 
} 


/** 
Returns the managed object model for the application. 
If the model doesn't already exist, it is created by merging all of the models found in the application bundle. 
*/ 
- (NSManagedObjectModel *)managedObjectModel { 

    if (managedObjectModel != nil) { 
     return managedObjectModel; 
    } 
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];  
    return managedObjectModel; 
} 


/** 
Returns the persistent store coordinator for the application. 
If the coordinator doesn't already exist, it is created and the application's store added to it. 
*/ 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Data.sqlite"]]; 

    NSError *error = nil; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { 
     /* 
     Replace this implementation with code to handle the error appropriately. 

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

     Typical reasons for an error here include: 
     * The persistent store is not accessible 
     * The schema for the persistent store is incompatible with current managed object model 
     Check the error message to determine what the actual problem was. 
     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return persistentStoreCoordinator; 
} 
+0

你可以顯示你的代碼:(1)設置你的託管對象模型(2)設置你的持久存儲協調器和(3)設置你的託管對象上下文? – 2010-03-22 00:42:49

+0

不要發佈問題的答案以添加更多信息。改爲編輯原始帖子。 – 2010-03-22 01:35:24

+0

遺憾我錯過了我可以編輯我原來的職位 – iosdevnyc 2010-03-22 01:37:52

回答

0
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Data.sqlite"]]; 

執行此行之後是什麼storeUrl價值?即你確定Data.sqlite存在於應用程序的Documents目錄中嗎?

所有這些代碼是否存在於您的應用程序委託中?如果您在managedObjectContext方法的頂部設置斷點,斷點是否會被觸發?在它被擊中後,你逐步瀏覽代碼,哪一行失敗?

+0

是在manaedObjectContent斷點被擊中 沒有一行是失敗 storeUrl值是正確的,因爲在RootViewController的表正在從sqlite的 – iosdevnyc 2010-03-22 01:45:35

+0

我沒有正確填寫理解。在你的問題中,你說「任何時候我去檢查我的NSManagedObjectContext它是返回null」(儘管我認爲你的意思是'nil')。你的意思是'managedObjectContext'在你的'rootViewController'裏面還是在你的應用程序委託裏? – 2010-03-22 01:58:07

+0

它在我的ServerModel類裏是零 – iosdevnyc 2010-03-22 02:41:44