Core Data, predicate using Key Path

Discussion in 'iOS Programming' started by Fuzej, Jul 20, 2012.

  1. Fuzej, Jul 20, 2012
    Last edited by a moderator: Jul 23, 2012

    Fuzej macrumors newbie

    Joined:
    Jul 20, 2012
    #1
    Literally from Developer Library:

    Recall that you can follow relationships in a predicate using a key path. The following example illustrates the creation of a predicate to find employees that belong to a department with a given name (but see also “Performance”).

    Code:
    NSString *departmentName = ... ;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
            @"department.name like %@", departmentName];
    If you use a to-many relationship, the construction of a predicate is slightly different. If you want to fetch Departments in which at least one of the employees has the first name "Matthew," for instance, you use an ANY operator as shown in the following example:

    Code:
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
        @"ANY employees.firstName like 'Matthew'"];
    _________________________________________

    Now, I dont understand how Core Data is able to know we are searching for employees in
    Code:
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
            @"department.name like %@", departmentName];
    and we are searching for departments in
    Code:
    Predicate *predicate = [NSPredicate predicateWithFormat:
        @"ANY employees.firstName like 'Matthew'"];
     
  2. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #2
    I think the documentation is assuming you're adding the predicates to instances of NSFetchRequest. You would have set the specific entity on the request.
     
  3. Fuzej thread starter macrumors newbie

    Joined:
    Jul 20, 2012
    #3
    If I have an entity, Employees, and an recursive relationship Supervision, so you have a supervisor and a supervisee.
    What if I want to fetch all supervisees of one specific supervisor ?
    What would the predicate look like ?
     
  4. Fuzej thread starter macrumors newbie

    Joined:
    Jul 20, 2012
    #4
    After reading some documentation this still isn't clear for me.
    Im a real newb if it comes to Core Data, can you please be more specific ?
     
  5. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #5
    Remember that you ask your managed object context to execute a fetch request, which is an instance of NSFetchRequest. You need to set the entity that you're requesting on your fetch request, it would typically look something like:

    Code:
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    fetchRequest.entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.moc];
    
    This is setting up for you fetch request to fetch Employee entities. The next thing you [usually] do is construct the predicate for the request. The predicate is applied to the entity you've specified in the request, which in this example is Employee:

    Code:
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.superviser == %@", self.supervisor];
    fetchRequest.predicate = predicate;
    
    If I understand your situation, this predicate should be applied to all Employee entities and only return Employees who's supervisor is the Employee in "self.supervisor".

    Edit: On-the-fly typing. The code may contain errors.
     
  6. Fuzej thread starter macrumors newbie

    Joined:
    Jul 20, 2012

Share This Page