Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Fuzej

macrumors newbie
Original poster
Jul 20, 2012
12
0
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'"];
 
Last edited by a moderator:
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.
 
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 ?
 
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.

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 ?
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.