fetch and predicate help

  • 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
  • On Sep 5, 2007, at 9:57 PM, Gonzalo Castro wrote:

    > 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?
    >
    No (<http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdFAQ.html#//apple_ref/doc/uid/TP40001802-242985
    >).

    > If so, what's the advantage of doing a fetch vs accessing the
    > instance's property directly?
    >
    None -- in fact it is counter-productive.  There's considerably
    greater overhead in executing a fetch than simply accessing the
    instance variable.

    mmalc
  • On Sep 6, 2007, at 1:02 AM, mmalc crawford wrote:

    >
    > On Sep 5, 2007, at 9:57 PM, Gonzalo Castro wrote:
    >
    >> 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?
    >>
    > No (<http://developer.apple.com/documentation/Cocoa/Conceptual/
    > CoreData/Articles/cdFAQ.html#//apple_ref/doc/uid/TP40001802-242985>).

    Ok, this makes sense. I'd never noticed that FAQ before, thanks.

    Continuing with the Department <-->> Employee example what if I have
    an Employee<-->>Patent inverse relationship where Patent is another
    entity.  (Employees can not share patents but can each have multiple
    patents in this contrived example.) How would I find employees in a
    specific department who have no patents. This is similar to my
    question a few days ago where I was looking for employees with no
    company cars but company cars was a to-one relationship so testing
    for NIL was ok. 'Patents' however is a to-many relationship. What
    would the predicate look like for that query? The "(patents == NIL)"
    test below is wrong.

    NSEntityDescription *entityDescription = [NSEntityDescription
    entityForName: @"Employee" inManagedObjectContext: moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity: entityDescription];
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(patents
    == NIL) AND (department == %@)", selectedDepartment];

    Thanks again,
    Gonzalo

    >
    >> If so, what's the advantage of doing a fetch vs accessing the
    >> instance's property directly?
    >>
    > None -- in fact it is counter-productive.  There's considerably
    > greater overhead in executing a fetch than simply accessing the
    > instance variable.
    >
    > mmalc
    >
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today