温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何实现COCaData的版本迁移

发布时间:2020-06-12 06:24:18 来源:网络 阅读:776 作者:周卫斌 栏目:开发技术

第一步:选中你要迁移的cocaData文件,然后依次点击点击上方的editor、Add Model Version。然后创建新的cocaData文件,文件名可以自己改写。


第二步:改变当前的版本,操作为,点击新创建的cocaData文件,然后点击右边的Model Version,选择为当前的文件名


第三步:找到下面这个方法

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

{

    if (_persistentStoreCoordinator != nil) {

        return _persistentStoreCoordinator;

    }

    

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Projec_B1.sqlite"];

    

    NSError *error = nil;

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

第四步: 创建字典:


   NSDictionary *optionsDic = @{NSInferMappingModelAutomaticallyOption:@(YES), NSMigratePersistentStoresAutomaticallyOption:@(YES)};


第五步: 然后把options的参数改为创建的字典:

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:optionsDic error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

        abort();

    }    

    return _persistentStoreCoordinator;

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI