Search code examples
iossortingcore-dataentity-relationshipnssortdescriptor

How to sort the relational data?


Only a little question:

I have an Entity X with an to-many relation to Entity Y

In the viewDidLoad method i fetch all X and sort them by name and ascending and they are displayed in a tableView.

If you choose one of the Xs in the tableView, all relational Y´s are shown in a second tableView. The result of this fetch also includes all relational Y´s for every X, so i dont have to do an extra fetch for the Y´s - but that leads to my Problem: The Y´s are in a completly random order (which also changes with every restart of the app) - but i want them sorted by date (<- a attribute in the Y Entity)... how can i do that?

Heres the fetch - code

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Customer" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
    // Handle the error.
}

[self setCustomersArray:mutableFetchResults];

and the result looks something like this:

Entity X-1
 attribute-x-1
 attribute-x-2
 relation-attribute
  relation-entity-y-1
   attribute-y-1
   attribute-y-2
  relation-entity-y-1
   attribute-y-1
   attribute-y-2
Entity X-2
 attribute-x-1
 attribute-x-2
 relation-attribute
  relation-entity-y-1
   attribute-y-1
   attribute-y-2

maybe thats an better example:

Customer
 name: Bert
 age: 10
 customerMeasureRealtions
  Measure
   score: 5
   date: 10.10.2010
  Measure
   score: 9
   date: 20.20.2000
Customer
 name: Steve
 age: 20
 customerMeasureRealtions
  Measure
   score: 7
   date: 07.07.2007

after the fetch the "Customers" are sorted by "name". But if i access the "Measures" through a "Customer" the order of the "Measures" is random, but i want them sorted by "date".

the code for accessing the Measures ("customer" is a Customer-Entity-Object or Model), ("measuresForCustomer" is the customerMeasureRealtions):

- (void)loadMeasureDataForCustomer:(Customer *)customer
{
NSSet *customerSet = customer.measuresForCustomer;
measuresArray = [NSMutableArray arrayWithArray:[customerSet allObjects]];
[measuresTable reloadData];
}

Solution

  • I think there are two ways you could do this:

    1. Initiate a second query to your db and fetch the "measures" for a given Customer. Have a look at Core data, sorting one-to-many child objects for this.
    2. Transfer the NSSet into an NSArray and sort it in place. An NSSet is always unordered. Have a look at How to sort a NSArray alphabetically?. Or Apple's documentation: sortedArrayUsingDescriptors in conjunction with a NSSortDescriptor.