Fastest way to check if an object exists in Core Data or not?
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];
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:
.
Edit: (by Regexident)
As Josh Caswell correctly commented, the correct way to handle errors is either this:
if (count == NSNotFound) {
NSLog(@"Error: %@", error);
return 0;
}
return count;
or this (without error logging):
return (count != NSNotFound) ? count : 0;
Yes, definitely there is a better method. Setup a fetch request as usual, but, instead of actually executing it, simply ask for the number of objects it would have returned if it had been passed to executeFetchRequest:error:
This can be done using
- (NSUInteger)countForFetchRequest:(NSFetchRequest *)request error:(NSError **)error;
Something like this:
- (int) numberOfContacts{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSManagedObjectContext *managedObjectContext = yourManagedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contacts" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSError *error = nil;
NSUInteger count = [managedObjectContext countForFetchRequest:request error:&error];
[request release];
if (!error){
return count;
}
else
return -1;
}