Cocoa Core数据高效的方法来计算实体! [英] Cocoa Core Data efficient way to count entities!
本文介绍了Cocoa Core数据高效的方法来计算实体!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谢谢
解决方案
我不知道是否使用NSFetchedResultsController是最有效的方式来实现你的目标(但它可能是)。获取实体实例计数的显式代码如下:
//假设NSManagedObjectContext * moc
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:moc]];
[request setIncludesSubentities:NO]; //省略子实体。默认值为YES(即包含子实体)
NSError * err;
NSUInteger count = [moc countForFetchRequest:request error:& err];
if(count == NSNotFound){
//处理错误
}
[request release];
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...
thank you
解决方案
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];
这篇关于Cocoa Core数据高效的方法来计算实体!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文