删除 NSManagedObjectContext 中的所有记录

发布于 2024-08-11 04:45:15 字数 521 浏览 1 评论 0原文

有没有办法从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一直都在从未离去 2024-08-18 04:45:15

要删除给定实体的所有实例(我们将使用您的购物篮),您只需获取所有购物篮然后将其删除即可。只需几行代码:

NSManagedObjectContext * context = [self managedObjectContext];
NSFetchRequest * fetch = [[[NSFetchRequest alloc] init] autorelease];
[fetch setEntity:[NSEntityDescription entityForName:@"ShoppingBasket" inManagedObjectContext:context]];
NSArray * result = [context executeFetchRequest:fetch error:nil];
for (id basket in result)
    [context deleteObject:basket];

非基于文档的应用程序中的替代方法是关闭与数据存储的连接,删除实际文件,然后重新连接(标准 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:

NSManagedObjectContext * context = [self managedObjectContext];
NSFetchRequest * fetch = [[[NSFetchRequest alloc] init] autorelease];
[fetch setEntity:[NSEntityDescription entityForName:@"ShoppingBasket" inManagedObjectContext:context]];
NSArray * result = [context executeFetchRequest:fetch error:nil];
for (id basket in result)
    [context deleteObject:basket];

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. :-)

归属感 2024-08-18 04:45:15

更快的方法是完全删除商店。这样,您就不会像其他答案那样浪费任何时间来获取对象或枚举它们。

NSError *error;
NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [applicationDocumentsDirectory URLByAppendingPathComponent:@"MyCDStore.sqlite"];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];

删除后不要忘记重新创建它。

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.

NSError *error;
NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [applicationDocumentsDirectory URLByAppendingPathComponent:@"MyCDStore.sqlite"];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];

Don't forget to re-create it after you have deleted it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文