删除 NSManagedObjectContext 中的所有记录
有没有办法从 NSManagedObjectContext
中删除所有记录?
我使用以下代码插入数据:
NSManagedObjectContext * context = [[NSApp delegate] managedObjectContext];
NSManagedObject * basket = nil;
basket = [NSEntityDescription insertNewObjectForEntityForName:@"ShoppingBasket"
inManagedObjectContext: context];
[basket setValue:[firstSelectedObject valueForKey:@"accessoryID"]
forKey: @"accessoryID"];
如何删除所有记录?我想要类似“删除:”功能的东西,但要删除所有内容。
Is there a way to delete all the records from an NSManagedObjectContext
?
I'm using the following code to insert data:
NSManagedObjectContext * context = [[NSApp delegate] managedObjectContext];
NSManagedObject * basket = nil;
basket = [NSEntityDescription insertNewObjectForEntityForName:@"ShoppingBasket"
inManagedObjectContext: context];
[basket setValue:[firstSelectedObject valueForKey:@"accessoryID"]
forKey: @"accessoryID"];
How do I delete all the records? I want something that's like the "remove:" function, but to remove everything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要删除给定实体的所有实例(我们将使用您的购物篮),您只需获取所有购物篮然后将其删除即可。只需几行代码:
非基于文档的应用程序中的替代方法是关闭与数据存储的连接,删除实际文件,然后重新连接(标准 Core Data 项目附带的模板代码将自动如果该文件不存在则创建该文件)。然后你就有了一家全新的、空荡荡的商店。
请注意,代码示例忽略任何可能的错误。不要那样做。 :-)
To delete all instances of a given entity (we'll use your ShoppingBasket), you can simply fetch all baskets then delete them. It's just a few lines of code:
The alternative in a non-document-based app is to shut down your connection to the data store, delete the actual file, then reconnect (the template code that comes with a standard Core Data project will automatically create the file if it's absent). You then have a brand new, empty store.
Note, the code example ignores any possible error. Don't do that. :-)
更快的方法是完全删除商店。这样,您就不会像其他答案那样浪费任何时间来获取对象或枚举它们。
删除后不要忘记重新创建它。
A much quicker way would be to just remove store entirely. This way you're not wasting any time fetching objects, or enumerating through them as the other answer does.
Don't forget to re-create it after you have deleted it.