删除/重置 Core Data 中的所有条目?

编程入门 行业动态 更新时间:2024-10-28 18:28:18
本文介绍了删除/重置 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 中的所有条目?

本文发布于:2023-10-23 15:52:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   Core   Data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!