如何通过删除App Update上的旧数据来迁移核心数据

时间:2013-09-04 16:20:23

标签: iphone ios objective-c core-data core-data-migration

您好我要在appstore中更新我的iOS应用程序,此更新包含数据库更改,现在如何通过删除应用程序更新中现有版本的旧数据库来迁移现有核心数据? 我提到了Core Data Migration tutorial

Core Data Migration Post

不幸的是没有用。任何帮助都提前得到赞赏

2 个答案:

答案 0 :(得分:8)

史密斯,
我假设你在xcdatamodel中做了一些架构更改 如果您已经将应用程序提交到使用早期型号版本的App Store,请始终在进行任何更改之前添加新的模型版本(选择name.xcdatamodeld,然后选择Editor->添加模型版本)。
然后,
从Core Data选项卡添加新文件,作为Mapping Model
选择,源模型(提交的应用程序正在使用的模型版本)
目的地模型(您已完成更改的型号版本)

你完成了!

答案 1 :(得分:2)

在应用自动迁移之前,您是否可能尚未创建新版本的数据库模型?

  1. 从项目资源管理器中选择[dbname] .xcdatamodeld。
  2. 选择编辑器 - >添加模型版本。
  3. 选择您当前型号的基础。
  4. 确保您具有automigrate选项,如下所示:

    -(NSPersistentStoreCoordinator *)storeCoordinator {
    
    if (storeCoordinator_ != nil) {
        return storeCoordinator_;
    }
    
    NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"[dbname].sqlite"]];
    
    NSDictionary* storeOptions = @{NSMigratePersistentStoresAutomaticallyOption : [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption : [NSNumber numberWithBool:YES]}; 
    
    NSError *error = nil;
    storeCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self objectModel]];
    
    if (![storeCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:storeOptions error:&error]) {
    
        // handle error here and remove abort
    
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    
    return storeCoordinator_;
    }
    
  5. 然后离开。