fetch and predicate help
fetch and predicate help
- Subject: fetch and predicate help
- From: Gonzalo Castro <email@hidden>
- Date: Thu, 6 Sep 2007 00:57:13 -0400
Hello,
I have these entities with inverse relationship: Department <-->>
Employee. I need to know if a specific department instance has zero
employees.
I can simply ask the department instance for its "employee" property
and check its count. This works fine but should I be using a fetch to
get this information instead? If so, what's the advantage of doing a
fetch vs accessing the instance's property directly?
If fetch is the way to go what would the predicate look like? Below
is my attempt at the predicate but it's clearly wrong. It's not a nil
employee property I'm looking for, I need to know if it contains any
items.
NSManagedObject *selectedDepartment = [self selectedDepartment]; //
this gives me a Department instance
if ( [[selectedDepartment valueForKey: @"employees"] count] == 0 )
{
/* selectedDepartment has no employees */
}
// the fetch that doesn't work
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName: @"Department" inManagedObjectContext: moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity: entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(employees == NIL) AND (SELF == %@)", selectedDepartment];
[request setPredicate: predicate];
NSError *error = nil;
NSArray *fetchResult = [moc executeFetchRequest: request error:
&error];
if ( [fetchResult count] > 0 )
{
/* selectedDepartment has no employees */
}
Thanks,
Gonzalo
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden