OGeek|极客世界-中国程序员成长平台

标题: ios - 如何*跟踪* CoreData 中的对象计数 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:17
标题: ios - 如何*跟踪* CoreData 中的对象计数

我想跟踪特定托管对象的数量。 NSFetchedResultsController 的方式相同,只是我不需要返回任何数据,我只需要一个数字。最有效的方法是什么?

附带说明一下,显然我不想以最直接的方式使用 NSFetchedResultsController,因为它会无缘无故地造成一堆错误并阻塞内存。

我试图通过结合 NSCountResultTypeNSFetchedResultsController 来实现这一点,但是在合并上下文时我遇到了一些奇怪的崩溃:

NSError *error;
auto defaultContext = [NSManagedObjectContext MR_defaultContext];
auto fetchRequest = [NotificationModel MR_requestAllWhere:NotificationModelAttributes.isRead isEqualToNO];

fetchRequest.includesSubentities = NO;
fetchRequest.includesPropertyValues = NO;
fetchRequest.resultType = NSCountResultType;
fetchRequest.sortDescriptors = @[];
fetchRequest.propertiesToFetch = @[];

self.notificationCountFetchController = [NotificationModel MR_fetchController:fetchRequest delegate:self useFileCache:NO groupedBy:nil inContext:defaultContext];
[self.notificationCountFetchController performFetch:&error];
[MagicalRecord handleErrors:error];



Best Answer-推荐答案


MagicalRecord 中的所有更改都经过根上下文,因此我注册了 NSManagedObjectContextWillSaveNotification 观察并在每次保存时获取对象计数。很简单。

关于ios - 如何*跟踪* CoreData 中的对象计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32606041/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4