【问题标题】:Possible downside of manually deleting old xcdatamodel of a shipped app手动删除已发布应用程序的旧 xcdatamodel 的可能缺点
【发布时间】:2012-09-28 19:00:20
【问题描述】:

我有一个已经发布的 CoreData 应用程序,在发布一个(版本 10)之前,我有 9 个模型过时的版本。 我想删除将我引向最终模型的一长串开发模型。

在开发过程中,我发现这可以通过删除 myproject.xcodeproj 中的引用并同时删除 mydata.xcdatamodeld 中的 version_x.xcdatamodel 文件来轻松完成。

但是我正在尝试调查任何可能的缺点,特别是考虑到我的应用已经在应用商店中。 考虑到我无法从早期版本迁移或恢复数据模型。

相反,我刚刚添加了一个基于版本 10 的新模型,称为版本 11,我正在对其进行开发。 我不知道迁移的机制,但为什么我需要版本 10 之前的模型?

【问题讨论】:

  • 您确定您的所有用户都已升级到最新版本吗?
  • 这是一个非常好的观点,我完全怀念。但是,这不是我的情况,因为版本 10 是起始版本。
  • thisthis

标签: objective-c xcode core-data


【解决方案1】:

首先制作数据模型的多个版本的主要原因是为了更优雅地处理迁移(升级数据库),而不是仅仅删除现有数据库并使用更改创建一个新数据库。

因此,如果您之前发布的应用版本使用了之前的数据模型,并且您希望能够优雅地升级数据库,那么只需保持之前的模型不变。

如果您创建多个版本的数据模型的唯一原因是在初始开发期间保持数据完好无损,并且没有其他人拥有您的应用程序与以前的数据模型,那么请删除。没关系。

要自动优雅地迁移您的数据模型,请在您的应用委托中使用以下代码:

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"]; //change to the name of your database

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

        //[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]; --delete old model if necessary
        //abort(); --Abort App if necessary
    }

    return _persistentStoreCoordinator;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多