I am trying to figure out how to write the following SQL in the form of the Core Data fetch statements:
select count(*) from MYTABLE where MYTABLE.size='2';
All I have at the moment is the following:
//setup predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K == %@", @"size", searchInteger];
[request setPredicate:predicate];
NSError *error = nil;
NSInteger count = [managedObjectContext countForFetchRequest:request error:&error];
NSLog(@"fetch results count = %i", count);
[request release];
What I am not sure of is:
- Is this the most efficient / speedy way to do a select count(*) with a condition
- Is there any other way to setup the predicate here to be more efficient?
Any suggestions from the gurus welcome.
Thanks in advance.
Pras.
The
countForFetchRequest:is certainly the most efficient way to get the count. I would think that you should not use the keywordANYin your predicate format string, though.Also, make sure that your
searchIntegeris of typeNSNumber. If not you need to wrap it into something like[NSNumber numberWithInt:searchInteger].