2011-04-16 56 views
0

我得到了一個名爲TestProject的xcode4項目,即時嘗試添加CoreData。我用一些實體添加了一個數據模型(名爲TestDataModel),併爲這些實體創建了NSManagedObject類。IOS CoreData - 無法加載NSPersistentStoreCoordinator

我的問題是我不能裝載持久性存儲

if(self.managedObjectContext == nil){ 
     NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); 
     NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
     NSURL* storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingPathComponent: @"TestProject.sqlite"]]; 
     NSError* errors; 

     NSPersistentStoreCoordinator* persistentStoreCoordinator = 
          [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel 
                    mergedModelFromBundles:nil ]]; 

     if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                configuration:nil 
                   URL:storeUrl 
                  options:nil 
                  error:&errors]) 
     { 
      NSLog(@"Error loading persistent store: %@", [errors localizedDescription]); 
     } 

     self.managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [self.managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator]; 

    } 

我不斷收到錯誤:

Error loading persistant store: The operation couldn’t be completed. (Cocoa error 258.) 

請注意,我也是在模擬器中運行此。

在此先感謝。

回答

0

想通了。不知道我需要創建數據庫

下面的代碼工作

if(self.managedObjectContext == nil){ 

     NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"TestProject.sqlite"]; 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     if (![fileManager fileExistsAtPath:storePath]) { 
      NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"TestProject" ofType:@"sqlite"]; 
      if (defaultStorePath) { 
       [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
      } 
     } 

     NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 


     NSError* errors; 
     NSPersistentStoreCoordinator* persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]]; 

     if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&errors]) 
     { 
      NSLog(@"Error loading persistent store: %@", [errors localizedDescription]); 
     } 

     self.managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [self.managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator]; 

    } 

youll需要這個藏漢

- (NSString *)applicationDocumentsDirectory { 
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
} 
1

您使用了錯誤的目錄。

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); 
                ^^^^^^^^^^^^^^^^^^^^^^^^ 

變化成

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
                ^^^^^^^^^^^^^^^^^^^ 

這樣做的NSDocumentDirectory由iOS的創建,但NSDocumentationDirectory目錄不是。