2012-07-15 131 views
0

當我想創建持久存儲時出現錯誤。 當我爲iOS 5.0構建時,沒有錯誤,核心數據成功運行。核心數據 - 持久存儲無法在iOS 5.1上創建

下面是創建持久存儲的代碼:

+ (NSManagedObjectContext *)getContext { 

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, 
          [NSNumber numberWithBool:YES], 
          NSInferMappingModelAutomaticallyOption, nil]; 


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
    NSURL *storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingFormat:@"Database.sqlite"]]; 
    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]]; 
    NSError *error = nil; 

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 
     NSLog(@"error loading persistent store.."); 
     [[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil]; 
     if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      //abort(); 
     } 
    } 


    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; 
    [context setPersistentStoreCoordinator:persistentStoreCoordinator]; 

    return context; 


} 

錯誤日誌:

error loading persistent store.. 
2012-07-15 13:16:31.440 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xde88ca0 {reason=Failed to create file; code = 1}, { 
    reason = "Failed to create file; code = 1"; 
} 
2012-07-15 13:16:31.455 Partyfinder[8213:707] error loading persistent store.. 
2012-07-15 13:16:31.458 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xdeb5360 {reason=Failed to create file; code = 1}, { 
    reason = "Failed to create file; code = 1"; 
} 
2012-07-15 13:16:31.467 Partyfinder[8213:707] error loading persistent store.. 
2012-07-15 13:16:31.470 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xdeb6680 {reason=Failed to create file; code = 1}, { 
    reason = "Failed to create file; code = 1"; 
} 
2012-07-15 13:16:31.483 Partyfinder[8213:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.' 
*** First throw call stack: 
(0x358fa88f 0x37ca1259 0x36230fe7 0x36299287 0x593e5 0x45917 0x5bfc3 0x5c487 0x5bfc3 0x5bf6b 0x45285 0x5bfc3 0x5a883 0x5e26b 0x358ca433 0x3588f533 0x3588f77b 0x358cead3 0x358ce29f 0x358cd045 0x358504a5 0x3585036d 0x374ec439 0x3335ccd5 0x42119 0x420c4) 
terminate called throwing an exception 

任何人知道如何解決這個錯誤嗎?

+0

嘗試從Simulator/Device中刪除應用程序並再次運行,這可能會解決您的問題。謝謝 – SALMAN 2012-07-15 14:23:38

回答

0

的 「解決方案」 是,.sqlite文件必須具有相同的名稱作爲項目名稱!

0

我認爲有兩種可能的情況爲這個錯誤:

1)您創建的錯誤該文件在您的應用程序的以前的執行。如果您使用以下菜單重置模擬器,這將得到解決:iOS模擬器>重置內容和設置,並從設備上卸載應用程序:長按>單擊x符號。

2)在您的應用程序中有一些代碼創建此文件。如果是這種情況,您應該找到該代碼並將其刪除。

OR

你可以通過這個鏈接

Core Data Tips for iPhone Devs Part 2: Better Error Messages

感謝

+0

沒有幫助你的提示...爲解決方案看到我的答案 – 2012-07-17 11:49:25

相關問題