在iOS 7中的CoreData迁移 [英] CoreData migration in iOS 7

查看:225
本文介绍了在iOS 7中的CoreData迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后让这个迁移工作,这是一个很大的痛苦。

Finally got this migration working, it was a big pain.

源代码

在iOS6中一切正常,但在iOS7应用程序崩溃

Everything works fine in iOS6 but in iOS7 app crash

推荐答案

根据您的评论和错误报告, http://openradar.io/15555487我猜想整个消息是这样的:

Based on your comment and a bug report at http://openradar.io/15555487 I'm guessing that the full message is something like:

CoreData: error: failure reading metadata plist with data bytes: <33>
Unable to open database. Error: The file couldn’t be opened because it isn’t in the correct format.
Info: {
    NSUnderlyingException = "An error <null> occurred converting the metadata plist data: <33>";
}

同样基于该报告以及您的源代码:在迁移后留下旧的 wal shm 文件,导致迁移后出现问题。从iOS 7开始,Core Data使用WAL模式日记。这意味着有一些额外的文件,你需要担心当复制或删除持久存储。如果您的商店文件名为 foo.sqlite ,还有一个 foo.sqlite-wal

Also based on that report, plus your source code: you're almost certainly leaving old wal and shm files around after the migration, which are causing problems after the migration. Beginning with iOS 7, Core Data uses WAL-mode journaling. That means there are a couple of extra files you need to worry about when copying or removing a persistent store. If your store file name is foo.sqlite, there's also a foo.sqlite-wal and foo.sqlite-shm in the same directory.

您的代码会删除旧的SQLite文件,并将其替换为新的文件,但会离开现有的日志文件就位。这导致数据不一致,因为日志文件不再对应于主SQLite文件。 SQLite和Core Data无法判断剩下的内容,因此应用程序崩溃。

Your code removes the old SQLite file and replaces it with the new one, but leaves the existing journal files in place. That causes inconsistent data, since the journal files don't correspond to the main SQLite file any more. SQLite and Core Data can't make sense of what's left, so the app crashes.

解决方案最有可能确保清理额外的文件。当您进行此调用时:

The solution is most likely to make sure to clean up the extra files. When you make this call:

success = [[NSFileManager defaultManager] removeItemAtURL:storeURL error:error];

...也会移除这两个额外的档案。

...also remove the two extra files.

对于将来的工作,将持久存储文件放在其自己的目录中而不使用其他文件是有用的。然后,如果你需要删除存储,你只需一步删除目录,而不需要关心SQLite或Core Data可能创建的其他额外文件。

For future work, it's useful to put the persistent store file in its own directory with no other files. Then if you ever need to remove the store, you can just remove the directory in one step, with no need to care about what other extra files SQLite or Core Data might have created.

这篇关于在iOS 7中的CoreData迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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