如何实现COCaData的版本迁移

第一步:选中你要迁移的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;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章