【问题标题】:Core Data : Post migration, additional migration code核心数据:迁移后,附加迁移代码
【发布时间】:2011-04-07 02:14:43
【问题描述】:

我希望从我的版本 1 数据模型迁移到版本 2,但是一旦迁移完成,我希望执行一些自定义迁移代码。我如何知道是否/何时发生迁移?是否有 migrationHasCompleed 委托方法或通知?

出于利益考虑:我希望在数据库中执行调整 png 大小的自定义迁移代码。

【问题讨论】:

标签: iphone objective-c cocoa core-data


【解决方案1】:

好吧,您可以在迁移发生后立即在持久存储协调器设置上运行此代码:

+ (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

【讨论】:

  • 来自 Apple 文档:“在打开商店之前,您使用 isConfiguration:compatibleWithStoreMetadata: 检查其架构是否与协调器的模型兼容。...您可以简单地使用 addPersistentStoreWithType:configuration:URL:options :error: 检查是否需要迁移,但是这是一个重量级的操作,效率低下。”
【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-14
    • 2017-07-07
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 2011-07-22
    • 2023-03-14
    相关资源
    最近更新 更多