2016-03-19 125 views
1

我是Core Data的新手,我一直在試圖獲取我創建的實體,但我不斷收到拋出的錯誤消息。提取時的核心數據錯誤

的錯誤是如下:當我的視圖控制器上viewDidAppear被稱爲被拋出

CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/4A0BBC17-674F-4A5D-80F7-72174D71B95F/Documents/Todogorithm.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store." UserInfo={metadata={ 
    NSPersistenceFrameworkVersion = 641; 
    NSStoreModelVersionHashes =  { 
     Task = <3db44fe3 d21215ef eee9476a ae204647 b49829de 00b4784c 7715b5ff a252dcab>; 
    }; 
    NSStoreModelVersionHashesVersion = 3; 
    NSStoreModelVersionIdentifiers =  (
     "" 
    ); 
    NSStoreType = SQLite; 
    NSStoreUUID = "DFB8723E-7D5B-44A0-AFB9-E9E0F7E39E85"; 
    "_NSAutoVacuumLevel" = 2; 
}, reason=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary { 
    metadata =  { 
     NSPersistenceFrameworkVersion = 641; 
     NSStoreModelVersionHashes =   { 
      Task = <3db44fe3 d21215ef eee9476a ae204647 b49829de 00b4784c 7715b5ff a252dcab>; 
     }; 
     NSStoreModelVersionHashesVersion = 3; 
     NSStoreModelVersionIdentifiers =   (
      "" 
     ); 
     NSStoreType = SQLite; 
     NSStoreUUID = "DFB8723E-7D5B-44A0-AFB9-E9E0F7E39E85"; 
     "_NSAutoVacuumLevel" = 2; 
    }; 
    reason = "The model used to open the store is incompatible with the one used to create the store"; 
} 

錯誤。這裏是我的viewDidAppear: [super viewDidAppear:animated];

AppDelegate *appDelegate = [[AppDelegate alloc] init]; 

NSManagedObjectContext *context = [appDelegate managedObjectContext]; 

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Task"]; 
_results = [context executeFetchRequest:request error:nil]; 

的代碼中有調用我的appDelegate的managedObjectContext,並且該方法中的persistentStoreCoordinator方法被調用(這是錯誤實際上是拋出)。從AppDelegate.m這兩種方法是如下:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 
    // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. 
    if (_persistentStoreCoordinator != nil) { 
     return _persistentStoreCoordinator; 
    } 

    // Create the coordinator and store 

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Todogorithm.sqlite"]; 
    NSError *error = nil; 
    NSString *failureReason = @"There was an error creating or loading the application's saved data."; 
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { 
     // Report any error we got. 
     NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
     dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data"; 
     dict[NSLocalizedFailureReasonErrorKey] = failureReason; 
     dict[NSUnderlyingErrorKey] = error; 
     error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict]; 
     // Replace this 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. 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    return _persistentStoreCoordinator; 
} 


- (NSManagedObjectContext *)managedObjectContext { 
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) 
    if (_managedObjectContext != nil) { 
     return _managedObjectContext; 
    } 

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

任何想法,爲什麼persistentStoreCoordinator錯誤被稱爲?

+0

我想,你應該嘗試刪除/卸載這個應用程序,然後再運行它。 –

回答

6

您可能已在Xcode中更改了您的託管對象模型,但您仍然在裝有舊數據模型配置的設備/模擬器上安裝了應用程序。

這就是爲什麼你得到這個錯誤。 如果您的應用程序是全新的,而且您不必擔心與已安裝應用程序的兼容性,則可以從設備/模擬器中刪除舊應用程序並安裝新應用程序。

如果您需要保持舊版數據模型配置與新版數據模型配置的兼容性,則應該使用版本控制模型並在新版數據模型版本中進行所有更改。

2

從模擬器/設備中刪除應用程序,執行清理並重新構建您的項目。

每當您對Core Data定義執行更改時,刪除安裝在物理設備或模擬器上的應用程序,再次清理項目並重新構建。