Skip navigation.
 
mlRe: Counting instances in Core Data
FROM : Adam Swift
DATE : Tue Apr 29 20:33:27 2008

On Apr 29, 2008, at 11:18 AM, Adam Swift wrote:

>
> On Apr 29, 2008, at 9:03 AM, Steve Cronin wrote:

>>
>> My question is: what is the most efficient fetch to pose given that 
>> every fetch is IO.
>> A given entity might have a lot of records so an array COULD be an 
>> unnecessarily large transaction...
>>
>> Is there a best practice fetch for this 'get a count' purpose?

>


Sorry - I should have pointed this out- there are a lot of options 
available for tuning performance (fetch IDs, prefetch relationships, 
fetch in batches, etc, etc) and it definitely makes sense to measure 
before you start optimizing -- this covers the breadth and summarizes 
it very nicely:

http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles/cdPerformance.html#/
/apple_ref/doc/uid/TP40003468

> In Tiger that was the best means available, but if you're targeting 
> Leopard there is a better option available:
>
> Create your fetch request to fetch all instances of a given entity 
> and execute the fetch with countForFetchRequest:error: which will 
> simply return the number of instances/rows that match your query.
>
> request = [[[NSFetchRequest alloc] init] autorelease];
> [request setEntity:[NSEntityDescription entityForName:@"Foo" 
> inManagedObjectContext:context]];
> count = [context countForFetchRequest:request error:&error];
>
> http://developer.apple.com/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/Reference/Reference.html
>
> - adam

Related mailsAuthorDate
mlCounting instances in Core Data Steve Cronin Apr 29, 18:03
mlRe: Counting instances in Core Data I. Savant Apr 29, 18:08
mlRe: Counting instances in Core Data I. Savant Apr 29, 18:31
mlRe: Counting instances in Core Data Adam Swift Apr 29, 20:18
mlRe: Counting instances in Core Data I. Savant Apr 29, 20:33
mlRe: Counting instances in Core Data Adam Swift Apr 29, 20:33
mlRe: Counting instances in Core Data Ben Trumbull Apr 29, 22:57