应用更新时 iPhone 数据迁移 [英] Iphone data migration on application update

查看:25
本文介绍了应用更新时 iPhone 数据迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 sqlite 存储应用程序和用户数据的 iphone 应用程序.

I am working on an iphone application that uses sqlite to store application and user data.

这是应用程序的第一个版本,我想知道我需要做些什么来为应用程序的未来版本做准备.

This is the first version of the application and I wonder what I need to do to prepare for the future versions of the app.

这就够了吗?1. 确保 XCode 项目设置中的应用程序版本正确2.在sqlite文件中添加版本号,方便以后读取

Is this enough? 1. Make sure app version is correct in the XCode Project settings 2. Add a version number in sqlite file so that it can be read later

让我们模拟下一次更新!1. 新版app下载到设备2.应用程序读取之前存储在sqlite表/字段中的版本号3. 应用程序知道应用程序数据已更改,因此需要更新sqlite,并且它想要保留用户数据(在应用程序收藏夹和注释中).4. Schema 没有改变,所以它开始将新的应用程序数据插入到一些表中.5. 不接触用户数据表6. 更新sqlite db的版本号.

Lets simulate the next update! 1. New version of the app is downloaded to the device 2. The app reads the version number stored earlier in a sqlite table/field 3. App knows that application data has changed so it needs to update the sqlite and also it wants to retain the user data (in app favorites & Notes). 4. Schema has not changed so it starts to insert the new application data into some tables. 5. It does not touch the user data tables 6. It updates the version number of the sqlite db.

我是否错过了一些在更新时会回来咬我的东西?

Have I missed something that will come back and bite me when it is time for update?

推荐答案

因此,当您开发使用 Core Data 的应用程序时,您就开始定义 Core Data 模型.每个模型都有自己的版本.您开始创建模型的第一个且唯一的版本.您可以在 官方 Apple Core Data Model Versioning and Data Migration Programming Guide

So, when you develop an application that uses Core Data you start defining a Core Data Model. Each model will have its own version. You start creating the first and unique version of your model. You can find all the required, detailed, info at the official Apple Core Data Model Versioning and Data Migration Programming Guide

您不应该通过在数据库本身中设置的自定义字段来处理数据库版本控制,而是通过您的 Xcode 项目使用模型版本.

You should not handle database versioning through custom fields set in the database itself, use model versions instead via your Xcode project.

更新应用程序时,如果您不更改模型版本,您不必担心任何事情,因为数据库没有被触及.每一个不改变模型的新更新都不需要您的努力.

When updating the application, if you do not change the model version you do not have to worry about anything, since the database hasn't been touched. Every new update that do not change the model will require no effort from you.

如果您需要为下一次更新更新模型,您必须这样做:

If you need to update your model for the next update you have to do this:

  1. 创建新模型版本
  2. 将新模型版本设置为活动版本
  3. 根据您的需要修改模型

有趣的部分来了.您的应用程序将以两种不同的方式处理模型之间的迁移:简单方式和困难方式.

Here comes the interesting part. Your application will handle the migration between the models in two different ways: the simple way and the hard way.

遵循 Apple 关于核心数据的官方文档模型版本控制和数据迁移,如果您的模型略有更改,您可以要求应用程序执行自动轻量级迁移.如果满足这些要求,此步骤会将现有数据库更新为新版本:

Following Apple's official documentation about Core Data Model Versioning and Data Migration, if your model has been slightly changed you can ask the application to perform an automatic lightweight migration. This step will update the existing database to the new version if these requirements are met:

  1. 简单地添加一个新属性
  2. 非可选属性变为可选
  3. 可选属性变为非可选,并定义默认值

如果可能,轻量级迁移过程将处理从一个模型版本迁移到另一个模型版本所需的适当步骤

The lightweight migration process will handle the appropriate required step to migrate from one model version to another, if possible

如果您无法执行自动轻量级迁移,则必须手动定义要更新的步骤模型版本到新的,使用 Mapping模型对象.这些对象将定义应用程序必须做什么才能从模型版本 x 迁移到模型版本 y.

If you can't perform an automatic lightweight migration you will have to manually define the step to update the model versions to the new one, using Mapping Model Objects. These objects will define what the application has to do to migrate from model version x to model version y.

这篇关于应用更新时 iPhone 数据迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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