2010-09-02 107 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