防止升级用户的 CoreData 崩溃 [英] Preventing a CoreData crash for upgrading users

查看:26
本文介绍了防止升级用户的 CoreData 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大约一年半前,我开发了一个应用程序,我将再次使用它.这是我开始学习 Swift 的项目,很明显,从那时起,语言和我的 Swift 能力都发生了很大变化.

I built an app about a year and a half ago that I'm coming back to. It was the project I cut my Swift teeth on and obviously a lot has changed since then, both in the language and my Swift abilities.

昨天,我第一次更新了我的单个 CoreData 模型以添加一个可选的字符串属性.我做了文件生成位,并确保在检查器列中正确选择了新的数据模型.

Yesterday for the first time, I updated my single CoreData model to add an optional string attribute. I did the file generation bit and made sure in the inspector column the new data model is appropriately selected.

在我的模拟器和测试设备上,我需要删除旧版本的应用程序才能安装新版本,否则我会崩溃.我认为这只是开发环境过程的一部分.如何确保升级用户在从 App Store 进行简单更新时不必删除和重新安装?我假设 Xcode/CoreData/Apple 已通过一些内部脚本或流程处理了此问题对用户不可见,它只是工作."但我想在此处发布此内容,以了解是否需要做任何其他事情来确保用户从 v1 平滑过渡到 v1.1.

On my Simulator and testing devices, I need to delete the old version of the app to install the new version or I get a crash. I assume that's just part of the development environment process. How can I ensure that upgrading users won't have to delete and reinstall when they do a simple update from the App Store? I assume that Xcode/CoreData/Apple have this handled with some internal scripts or processes that are invisible to the user, "it just works." But I wanted to post this here to understand if there's anything additional I need to do to ensure a smooth transition from v1 to v1.1 for the user.

正如我所提到的,我所做的只是一个可选的字符串列.我假设所有现有的用户数据都将迁移到新模式,新字段为 nil.

All I did was an an optional string column, as I mentioned. I assume that all existing user data will be migrated over to the new schema with the new field being nil.

这里的任何想法都将受到欢迎和赞赏.谢谢!

Any thoughts here would be very welcomed and appreciated. Thanks!

推荐答案

如果您的应用在升级时在模拟器中崩溃,您的用户也会崩溃.

If your app is crashing in the Simulator when upgrading, your users will have crashes too.

为避免这种情况,您需要确保按照以下步骤操作:

To avoid this, you need to make sure you follow these steps:

  1. 确保您不以任何方式更改数据模型的原始版本.
  2. 在 Xcode 中,选择您的 xcdatamodel 文件,然后从菜单中选择 Editor > Add Model Version...
  3. Xcode 将根据当前模型建议一个新的版本名称.记下新版本名称,然后单击完成".
  4. 再次选择 xcdatamodel 文件,转到文件检查器,然后在模型版本下,选择新版本名称以使其成为当前版本.
  5. 在项目导航器中,选择新版本的 xcdatamodel.添加您的属性.

按此顺序执行这些步骤很重要.如果在创建新模型或将其设为当前版本之前添加属性,则会发生崩溃.

It's important to follow these steps in this order. If you add your attribute before creating the new model or making it your current version, you will have crashes.

这仅在您启用轻量级迁移时才有效.这是如何执行此操作的代码片段:

This will only work if you enable lightweight migrations. This is a code snippet of how to do this:

let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]

do {
    //coordinator is an NSPersistentStoreCoordinator
    try coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options
} catch var error as NSError {

  // handle error however you want here...            
  abort()
}

这篇关于防止升级用户的 CoreData 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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