Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between NSCountResultType and countForFetchRequest:error: in terms of performance?

I used to use NSCountResultType to count entities in my CoreData contexts. A while ago I encountered countForFetchRequest:error: which seems to do the same thing with just another (more readable) syntax.

Is there any difference between these two in terms of performance, memory management or other aspects despite the syntax?

like image 785
Carsten Avatar asked Jun 30 '13 08:06

Carsten


1 Answers

There seems to be no difference at all (only that one returns an NSUInteger and the other returns an NSArray containing an NSNumber).

Setting the launch argument

-com.apple.CoreData.SQLDebug 3

reveals that both

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Event"];
NSUInteger count = [context countForFetchRequest:request error:NULL];

and

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Event"];
[request setResultType:NSCountResultType];
NSArray *result = [context executeFetchRequest:request error:NULL];

execute exactly the same SQLite statement

SELECT COUNT( DISTINCT t0.Z_PK) FROM ZEVENT t0
like image 177
Martin R Avatar answered Nov 17 '22 05:11

Martin R