views:

391

answers:

1

I want to see if an object is persistet in Core Data or not. For example, I have Friends in Core Data, and I identify them by firstName. I can query core data to see if "George" is known. If the result set array contains more than zero objects, I know George is there. But Core Data loads the whole thing into memory, and I actually just want to know if George is stored or not.

How would I do it the most efficient way?

+9  A: 

Setup a Core Data request and, instead of actually issuing the query, do the following:

    NSError *error = nil;
 NSUInteger count = [managedObjectContext countForFetchRequest:request error:&error];
 [request release];

 if (!error){
  return count;
 }
 else
  return 0;

In practice, the method countForFetchRequest:error: returns the number of objects a given fetch request would have returned if it had been passed to executeFetchRequest:error:.

unforgiven
and it should be more efficient because under the hood, the SQL engine should be doing and COUNT instead of a SELECT
Ken Aspeslagh