Hi, I would like to know how to tell FetchRequest where I can order the objects in relation.
| Parent | | Child | | - name |------->| - name | | - position | | - position |
For example, if I have a parent table that contains a position attribute and has a one to many relationship with a child table that also has a position attribute. How to return parent objects (ordered by position) containing child objects ordered by position.
eg
parent 1 child 1 child 2 child 3 parent 2 child 15 child 16 parent 3 child 22 child 23 child 24
Obviously, the code below will correctly order the parent objects, but how to make the returned child objects with each parent in the correct order.
NSFetchRequest* fetchReqest = [[NSFetchRequest alloc] init]; NSEntityDescription* entity = [NSEntityDescription entityForName:@"parent" inManagedObjectContext:managedObjectContext]; [fetchReqest setEntity:entity]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES]; [fetchReqest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; [sortDescriptor release]; NSArray* parentsThatContainChildren = [managedObjectContext executeFetchRequest:fetchReqest error:nil];
Greetings
source share