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

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

问题描述

您知道以任何方式删除存储在Core Data中的所有条目吗?我的架构应该保持不变;



b

我希望通过编程来实现,因此用户基本上可以点击 reset 按钮。

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

然后,只需添加持久性存储,以确保重新正确地重新创建。



对每个实体进行迭代的编程方式更慢,容易出错。这样做的用法是,如果你想删除一些实体而不是其他实体。但是,您仍然需要确保保留引用完整性,否则您将无法保存更改。



只需删除商店并重新创建即可,既快捷又安全


$ b

iOS5的更新



随着在iOS 5和OS X 10.7中引入外部二进制存储(允许外部二进制数据存储或外部记录文件存储),只需删除由storeURL指向的文件是不够的。您将保留外部记录文件。由于这些外部记录文件的命名方案不是公共的,我还没有一个通用的解决方案。 - an0 5月8日'12在23:00


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.


Edit

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

解决方案

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.

Update for iOS5+

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天全站免登陆