可可核心数据有效的实体计数方法 [英] Cocoa Core Data efficient way to count entities

查看:88
本文介绍了可可核心数据有效的实体计数方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多有关Core Data的文章.但是对Entity-Type进行计数的一种有效方法是什么(例如SQL可以对SELECT count(1)进行处理...).现在,我只需用NSFetchedResultsController选择所有对象并获取NSArray的计数即可解决此任务!我敢肯定这不是最好的方法...

I read much about Core Data.. but what is an efficient way to make a count over an Entity-Type (like SQL can do with SELECT count(1) ...). Now I just solved this task with selecting all with NSFetchedResultsController and getting the count of the NSArray! I am sure this is not the best way...

推荐答案

我不知道使用NSFetchedResultsController是否是实现目标的最有效方法(但可能如此).获取实体实例数量的显式代码如下:

I don't know whether using NSFetchedResultsController is the most efficient way to accomplish your goal (but it may be). The explicit code to get the count of entity instances is below:

// assuming NSManagedObjectContext *moc

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:moc]];

[request setIncludesSubentities:NO]; //Omit subentities. Default is YES (i.e. include subentities)

NSError *err;
NSUInteger count = [moc countForFetchRequest:request error:&err];
if(count == NSNotFound) {
  //Handle error
}

[request release];

这篇关于可可核心数据有效的实体计数方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆