2012-01-19 29 views
0

在我看來無法解決的另一個奇怪錯誤之後,我絕對用Core Data拉我的頭髮。在遷移過程中發生奇怪的核心數據錯誤

這將是數據模型的第四個版本,以前的遷移已經奏效(雖然有一些令人頭痛的問題)。

我想要做的就是將字符串類型的屬性添加到'引擎'實體。我基於當前版本(v3)創建新版本的模型(版本4)。我選擇新創建的版本4作爲「當前模型」,並將字符串屬性添加到引擎實體。然後我創建一個新的映射模型,使用v3作爲源,v4作爲目標。我刪除了以前的引擎NSManagedObject子類,並使用新的修改後的引擎實體創建一個新引擎,檢查以確保新的String屬性位於頭文件中。我清理建立應用程序並運行它,並繁榮!我得到這個錯誤,約18倍:

{NSDetailedErrors =( 「錯誤域= NSCocoaErrorDomain代碼= 1570 \」 操作couldn \ U2019t完成(可可錯誤1570)\」的UserInfo = 0x6015820 {。 NSValidationErrorObject =(實體:BodySet; ID:0x60ba670;數據:),NSValidationErrorKey = availableCar,NSLocalizedDescription =操作couldn \ U2019t完成(可可錯誤1570)}」,

BodySet是在另一個實體。模型,但我在這次遷移過程中沒有觸及它,那麼爲什麼會導致所有這些錯誤?

我不知道如果這是一個幫助或沒有,但這裏是我的核心數據代碼:

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

- (NSManagedObjectModel *)managedObjectModel { 

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

    NSString *path = [[NSBundle mainBundle] pathForResource:@"CoreDataTest" ofType:@"momd"]; 
    NSURL *momURL = [NSURL fileURLWithPath:path]; 
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL]; 

    return managedObjectModel; 
} 

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 
    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 
    stringByAppendingPathComponent:@"<Project Name>.sqlite"]]; 

    //get the DB from the Documents directory: 
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] 
              stringByAppendingPathComponent: @"<Project Name>.sqlite"]]; 
    NSLog(@"Loading DB at path: %@", [storeUrl path]); 

    NSError *error = nil; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] 
           initWithManagedObjectModel:[self managedObjectModel]]; 

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


    if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
              configuration:nil URL:storeUrl options:options error:&error]) { 

     /*Error for store creation should be handled in here*/ 
     NSLog(@"Something went wrong....%@", [error description]); 
    } 

    return persistentStoreCoordinator; 
} 

任何幫助,這是非常讚賞。

回答

0

我已修復它,也許您可​​以將「availableCar」項目更改爲可選