2010-09-02 167 views
3

我想從我的VERSION1數據模型版本2遷移,但一旦遷移完成我想執行一些自定義的遷移代碼。我將如何知道是否/何時發生遷移?是否有migrationHasCompleed委託方法或通知?核心數據:遷移後,額外的遷移代碼

對於利益着想:自定義遷移代碼我希望進行重新調整PNG的數據庫。

+0

檢查:http://stackoverflow.com/questions/3025742/detecting-a-lightweight-core-data-migration – flypig 2014-01-08 05:55:57

+0

也檢查:http://www.ioscodingtips.com/lightweight-migration -with核心數據/ – flypig 2014-01-08 06:07:27

回答

0

嗯,你可以運行此代碼遷移之後發生的,在你的持久存儲協調設置:

+ (NSPersistentStoreCoordinator *)persistentStoreCoordinatorForStore:(NSString *)store { 
    NSURL *storeUrl = [NSURL fileURLWithPath:[[[self class] applicationDocumentsDirectory] stringByAppendingPathComponent:store]]; 

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

    // Check for model changes without trying to update 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { 
     // Set the automatic update options for the current model 
     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]) { 

      NSLog(@"Error opening the database. Deleting the file and trying again."); 

      //delete the sqlite file and try again 
      [[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil]; 
      if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options 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(); 
      } 
     } else { // The model was updates successfuly 
      // Implement here your custom migration code 
     } 

    }  

    return [persistentStoreCoordinator autorelease]; 
} 

乾杯,

VFN

+0

謝謝,我給它一個去... – user139816 2010-09-02 10:32:37

+0

從Apple文檔:「開店之前使用isConfiguration:compatibleWithStoreMetadata:以檢查它的模式是否與協調的模式兼容..你可以簡單地使用addPersistentStoreWithType:配置:網址:選項:錯誤:檢查是否需要遷移,但是這是一個重量級的操作和低效率的用於此目的「。 – cocoafan 2011-11-05 11:11:37

2

僅供參考,您還可以測試推進是否有必要進行移徙,這可能會更清潔。

NSError *error; 
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType 
                          URL:storeURL 
                         error:&error]; 
NSManagedObjectModel *destinationModel = [persistentStoreCoordinator managedObjectModel]; 
BOOL migrationRequired = ![destinationModel isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata]; 

// Now add persistent store with auto migration, and do the custom processing after