核心数据:迁移后,其他迁移代码 [英] Core Data : Post migration, additional migration code

查看:126
本文介绍了核心数据:迁移后,其他迁移代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wish to migrate from my version1 data model to version2, but once the migration is complete I wish to perform some custom migration code. How will I know if/when the migration occurs? Is there a migrationHasCompleed delegate method or notification?

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

For interests sake: The custom migration code I wish to perform resizes png's in the database.

推荐答案

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

Well, you could run this code right after the migration happen, on your persistent store coordinator setup:

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

这篇关于核心数据:迁移后,其他迁移代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆