Master data and NSSortDescriptor are not sorted according to the third NSString based descriptor

I have an NSFetchRequest that retrieves an object called "Player", and I want the results to be sorted by 3 attributes in the following order:

  • Does the "sendoffOffence" attribute mean zero or not
  • Regardless of whether the relation in the object in the team.homeMatch is zero or not
  • I want the rows in the number attribute to be sorted in ascending order.

Basically, I’m looking for all the players who have a red card (“sending OffOffence” not zero) to be displayed at the top of the list, and then set the set, regardless of whether the player is in the home team or not, and then finally, get a set of players in the team in the group to combat them, will be sorted by their knitwear numbers.

Thus, I use the following code in my fetch request:

// Set the entity for the fetch request NSEntityDescription *entity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:self.match.managedObjectContext]; [fetchRequest setEntity:entity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"match == %@", self.match]; [fetchRequest setPredicate:predicate]; // Sort the results // Sort according to whether the user has a red card / sendingOffOffence NSSortDescriptor *hasRedCard = [[NSSortDescriptor alloc] initWithKey:@"sendingoffOffence" ascending:NO]; // Sort according to whether the user is on the home team or not NSSortDescriptor *isHomeTeam = [[NSSortDescriptor alloc] initWithKey:@"team.homeMatch" ascending:NO]; // Sort according to the player jersey numbers NSSortDescriptor *number = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES selector:@selector(localizedStandardCompare:)]; [fetchRequest setSortDescriptors:@[hasRedCard, isHomeTeam, number]]; // Set the amount of records to retrieve from the database [fetchRequest setFetchBatchSize:20]; NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.match.managedObjectContext sectionNameKeyPath:nil cacheName:@"MatchCache"] 

However, when I execute the above code, I get the following results:

red cards sortyellow card sort

This sort order is incorrect because I want the following order to appear in the red card section:

  • 11 - Home
  • 12 - Home
  • 0 - Away
  • 11 - Away
  • 21 - Away

And in the yellow card section:

  • 1 - Home
  • 2 - Home
  • 99 - Home
  • 1 - Away
  • 31 - Away

It looks like the yellow card section is sorted correctly, but the red card section shows very strange behavior, which makes it seem like it is not sorted at all.

I'm pretty surprised why the red card is not sorted correctly - any thoughts? Should I just sort these objects in memory instead of relying on basic data to get my preferred order?

Note that this is the main data application with a repository of stored SQL repositories.

UPDATE

The following is the SQL statement that kernel data uses in my sample:

 CoreData: annotation: fetch using NSSQLiteStatement <0x11c77cd0> on entity 'SPlayer' with sql text 'SELECT t0.Z_ENT, t0.Z_PK FROM ZFSMANAGEDOBJECT t0 LEFT OUTER JOIN ZSTEAM t1 ON t0.ZTEAM = t1.Z_PK WHERE ( t0.ZMATCH1 = ? AND t0.Z_ENT = ?) ORDER BY t0.ZSENDINGOFFOFFENCE DESC, t1.ZHOMEMATCH DESC, t0.ZNUMBER COLLATE NSCollateFinderlike ' 

+4
source share
2 answers

This is a line that does not apply properly (for the case of a RED card):

 NSSortDescriptor *isHomeTeam = [[NSSortDescriptor alloc] initWithKey:@"team.homeMatch" ascending:NO]; 

Without seeing your data model and some example data, it's hard to say why this will not be sorted correctly. I would suggest that this would first put the NIL values ​​and then the correct values ​​(so sorting in increasing NO would do what it does in the case of the YELLOW card).

If I were in this situation, I would check my assumptions about the sort order and these team.homeMatch properties. Conduct a test where this is the only description of the description.

Then focus on what differs from the terms of the RED / YELLOW card.

+3
source

We did this without the NSFetchedResultsController, as this will test it ....

 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:self.match.managedObjectContext]; [fetchRequest setEntity:entity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"match == %@", self.match]; [fetchRequest setPredicate:predicate]; // Sort the results // Sort according to whether the user has a red card / sendingOffOffence NSSortDescriptor *hasRedCard = [[NSSortDescriptor alloc] initWithKey:@"sendingoffOffence" ascending:NO]; // Sort according to whether the user is on the home team or not NSSortDescriptor *isHomeTeam = [[NSSortDescriptor alloc] initWithKey:@"team.homeMatch" ascending:NO]; [fetchRequest setSortDescriptors:@[hasRedCard, isHomeTeam]]; // Set the amount of records to retrieve from the database [fetchRequest setFetchBatchSize:20]; NSArray *resultArray = [self.match.managedObjectContext executeFetchRequest:request error:&error]; NSSortDescriptor *number = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES selector:@selector(localizedStandardCompare:)]; NSArray *sortedArray = [resultArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:number, nil]]; 
+1
source

Source: https://habr.com/ru/post/1488401/


All Articles