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

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

问题描述

大约一年半以前,我开发了一个应用程序,后来又回来了.这是我咬牙切齿的项目,自那时以来,无论是语言还是我的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文件,然后从菜单中选择编辑器">添加模型版本..."
  3. Xcode将根据当前模型建议一个新的版本名称.记下新版本名称,然后单击完成".
  4. 再次选择xcdatamodel文件,转到文件"检查器,然后在模型版本"下,选择新版本名称,以使其成为当前版本.
  5. 在Project Navigator中,选择新版本的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天全站免登陆