核心数据:如何删除和重建数据存储? [英] Core Data: how to just delete and rebuild the data store?

查看:71
本文介绍了核心数据:如何删除和重建数据存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不需要保存用户数据的iOS 7+应用中使用了核心数据,该应用需要的所有数据都需要提供服务,并且可以随时恢复。因此,如果我在下一个应用程序更新中更改数据模型,则在删除所有先前的数据并再次请求所有数据时都没有问题。但是我不知道如何简单地用新模型替换以前的数据模型,而不执行迁移,因为看起来我不需要这样做...

I'm using Core Data in an iOS 7+ app that does not need to save user's data, all data the app needs is requested to services and it can be recovered at any time. So, if I change my data model in a next app update, I have no problem in deleting all the previous data and requesting it all again. But I don't know how to simply replace the previous data model with the new one, without performing a migration since it looks that I don't need to do that...

预先感谢

推荐答案

案例1:您正在使用SQLite存储

这适用于您的商店类型为 NSSQLiteStoreType 的情况。即使您计划不时删除数据,坚持使用SQLite也不是一个坏主意,因为它可以让您灵活地将缓存的数据保留在磁盘上任意长的时间,并且仅在更改时才删除它。模型,您不想应用任何迁移。

This applies if your store type is NSSQLiteStoreType. Even if you plan to delete your data from time to time, it's not a bad idea to stick to SQLite, as it gives you the flexibility to keep your cached data on disk as long as you wish, and only delete it when you change your model and you don't want to apply any migrations.

快速解决方案?初始化核心数据时,请在启动时删除您的 NSPersistentStoreCoordinator 的商店。
例如,如果您使用的是默认的SQLite存储,由Apple的示例代码提供:

Quick solution? Delete your NSPersistentStoreCoordinator's store at startup, when you're initializing Core Data. For instance, if you're using the default SQLite store, provided by Apple's boilerplate code:

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"cd.sqlite"]

您可以简单地删除文件:

you can simply delete the file:

[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];

然后使用 storeURL 添加新的持久性

您的 NSPersistentStoreCoordinator 不会抱怨如果您有新型号,也不需要任何迁移,但是您的数据将

then use storeURL to add a new persistent store as usual.
Your NSPersistentStoreCoordinator won't complain if you have a new model and you won't need any migrations, but your data will be lost, of course.

不用说,当您检测到数据模型中的变化时,您可以决定使用此解决方案,如果没有变化,就不用理会商店,以便您可以根据需要保留缓存的数据。

It goes without saying that you can decide to use this solution when you detect a change in the data model, leaving the store alone if there's no change, so that you can preserve your cached data as long as necessary.

更新:

TomHerrington建议在注释中,为确保完全删除了旧存储,还应删除日记文件,如果您不注意它们的话,将来可能会再次出现并困扰您。

如果您的商店文件名为 cd.sqlite ,如示例中所示,要删除的其他文件为 cd.sqlite-shm cd.sqlite-wal

As suggested by TomHerrington in the comments, to be sure you've removed the old store completely, you should also delete journal files, which might come back and haunt you in the future, if you don't take care of them.
If your store file is named cd.sqlite, like in the example, the additional files to be removed are cd.sqlite-shm and cd.sqlite-wal.

Core Data的WAL日记模式被默认引入如Apple在 QA1809 中所述,在iOS 7和OSX Mavericks中运行。

WAL journaling mode for Core Data was introduced as the default in iOS 7 and OSX Mavericks, as reported by Apple in QA1809.

案例2:使用内存存储区

您可以使用 NSInMemoryStoreType 而不是 NSSQLiteStoreType 切换到内存存储。在这种情况下,擦除存储要容易得多:您的所有数据都驻留在内存中,当您的应用停止运行时,所有数据都将消失,磁盘上将没有任何内容需要清理。下次,您可能会加载完全不同的模型,而无需进行任何迁移,因为没有要迁移的数据。

但是,此解决方案按原样实施,不会让您在会话之间缓存数据,看起来就像您希望在应用程序更新之间进行的操作(即,仅当应用程序更新并且模型更改时,才必须删除存储,否则将其保留在磁盘上可能很有用)。

As suggested, you could switch to an in-memory store, using NSInMemoryStoreType instead of NSSQLiteStoreType. Erasing the store is much easier in this case: all your data resides in memory, all of it will disappear when your app stops running, nothing will remain on disk for you to clean. Next time, you could potentially load a completely different model, without any migrations, as there's no data to migrate.
However, this solution, implemented as it is, wouldn't let you cache data between sessions, which looks like something you'd like to do between app updates (i.e., the store must be erased only when the app is updated and the model changes, while it could be useful to keep it on disk otherwise).

注意:

这两种方法都是可行的,各有利弊,我敢肯定可能还有其他策略。最后,您应该具有所有要素来决定特定情况下的最佳方法。

Both approaches are viable, with their pros and cons, and I'm sure there could be other strategies as well. In the end, you should have all the elements to decide what the best approach would be in your specific case.

这篇关于核心数据:如何删除和重建数据存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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