删除/重置 Core Data 中的所有条目? [英] Delete/Reset all entries in Core Data?

查看:30
本文介绍了删除/重置 Core Data 中的所有条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道有什么方法可以删除存储在 Core Data 中的所有条目吗?我的架构应该保持不变;我只想将其重置为空白.

Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank.

编辑

我希望以编程方式执行此操作,以便用户基本上可以点击 reset 按钮.

I'm looking to do this programmatically so that a user can essentially hit a reset button.

推荐答案

您仍然可以使用 NSFileManager:removeItemAtPath:: 方法以编程方式删除文件.

You can still delete the file programmatically, using the NSFileManager:removeItemAtPath:: method.

NSPersistentStore *store = ...;
NSError *error;
NSURL *storeURL = store.URL;
NSPersistentStoreCoordinator *storeCoordinator = ...;
[storeCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];

然后,只需重新添加持久存储以确保正确重新创建它.

Then, just add the persistent store back to ensure it is recreated properly.

迭代每个实体的编程方式既慢又容易出错.这样做的用途是如果您想删除某些实体而不是其他实体.但是,您仍然需要确保保留参照完整性,否则您将无法保留更改.

The programmatic way for iterating through each entity is both slower and prone to error. The use for doing it that way is if you want to delete some entities and not others. However you still need to make sure you retain referential integrity or you won't be able to persist your changes.

只需删除存储并重新创建它既快速又安全,当然可以在运行时以编程方式完成.

Just removing the store and recreating it is both fast and safe, and can certainly be done programatically at runtime.

iOS5+ 更新

随着 iOS 5 和 OS X 10.7 引入外部二进制存储(allowsExternalBinaryDataStorage 或 Store in External Record File),仅仅删除 storeURLs 指向的文件是不够的.您将留下外部记录文件.由于这些外部记录文件的命名方案是不公开的,所以我还没有一个通用的解决方案.– an0 2012 年 5 月 8 日 23:00

With the introduction of external binary storage (allowsExternalBinaryDataStorage or Store in External Record File) in iOS 5 and OS X 10.7, simply deleting files pointed by storeURLs is not enough. You'll leave the external record files behind. Since the naming scheme of these external record files is not public, I don't have a universal solution yet. – an0 May 8 '12 at 23:00

这篇关于删除/重置 Core Data 中的所有条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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