Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to query the row count from Core Data?

I am trying to get the number of rows for a given request. The only obvious way I've found to accomplish it is:

NSManagedObjectContext *context;
NSFetchRequest *request;

  ...

NSInteger count = [[context executeFetchRequest:request error:&error] count];

This seems wasteful to me, building an entire array of a large database's objects, just to find out how many there are. Is there a better way to accomplish this, in a "Core Data" way?

Thanks for the help!

like image 325
Alex Avatar asked Aug 23 '09 18:08

Alex


1 Answers

NSManagedObjectContext has a method which evaluates the count for a fetch request:

- (NSUInteger)countForFetchRequest:(NSFetchRequest *)request error:(NSError **)error;

See the API documentation.

like image 81
Jim Correia Avatar answered Oct 18 '22 13:10

Jim Correia