简体   繁体   English

iPhone应用程序更新(在Sqlite上使用Core Data)

[英]iPhone application update (using Core Data on Sqlite)

I have an app which is using Core Data on Sqlite, Now I have a update which has some DB structure changes say adding a new table 我有一个在Sqlite上使用Core Data的应用程序,现在我有一个更新,该更新具有一些数据库结构更改,例如添加一个新表

I know when an app get updated, it updates the app binary only, nothing on document directory will be changed. 我知道当应用程序更新时,它仅更新应用程序二进制文件,文档目录上的任何内容都不会更改。

When the app gets updated and launchs at the first time and run 当应用更新并首次启动并运行时

[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

It will find the difference between the data model and DB structure in Sqlite and will throw an exception and quit. 它将在Sqlite中发现数据模型和DB结构之间的差异,并将引发异常并退出。

Error: "The model used to open the store is incompatible with the one used to create the store" 错误:“用于打开商店的模型与用于创建商店的模型不兼容”

So, can anyone here give me some idea how to update an app when there is a DB structure change? 因此,在数据库结构发生更改时,这里有人可以给我一些想法如何更新应用程序吗?

I think we can run a DB script to create that new table when it launchs the update at the first time. 我认为我们可以在第一次启动更新时运行数据库脚本来创建该新表。

But if there are other changes like changing the type of some fields or deleting some fields, and we need to migrate the old data, this is really a headache. 但是,如果还有其他更改,例如更改某些字段的类型或删除某些字段,并且我们需要迁移旧数据,这确实令人头疼。 In this case, Is the only way to do is creating a new app? 在这种情况下,唯一的方法就是创建一个新的应用程序吗?

Is there anyone tried something similar like this? 有没有人尝试过类似的东西?

You need to version your data model to handle any new fields you've added. 您需要对数据模型进行版本控制,以处理添加的所有新字段。 Apple has detailed documentation on this. 苹果对此有详细的文档 If you're just adding fields, it's pretty easy. 如果您只是添加字段,这很容易。 I haven't tried deleting fields, but I imagine it's also straightforward. 我没有尝试删除字段,但我想它也很简单。 Once you get into changing fields, you introduce a little complexity, but it's not too hard. 一旦进入不断变化的领域,就会引入一些复杂性,但这并不难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM