Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data and NSSortDescriptor not sorting according to third descriptor based on NSString
    primarykey
    data
    text
    <p>I have an NSFetchRequest that is fetching an entity called "Player" and I want the results be sorted by 3 attributes in the following order:</p> <ol> <li>Whether the attribute called "sendingoffOffence" is nil or not</li> <li>Whether the relationship in the entity at "team.homeMatch" is nil or not</li> <li>I want the strings in the "number" attribute to be sorted in ascending order.</li> </ol> <p>Basically, I am looking to have all players that have a red card ("sendingOffOffence" is not nil) appear at the top of the list, then have the set be ordered by whether the player is on the home team or not, and then finally get the set of players on a team in an offense group be sorted by their jersey numbers.</p> <p>As such, I use the following code in my fetch request:</p> <pre><code>// 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's 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"] </code></pre> <p>However, when I execute the above code, I get the following results:</p> <p><img src="https://i.stack.imgur.com/M3tig.png" alt="red cards sort"> <img src="https://i.stack.imgur.com/MxxeR.png" alt="yellow card sort"></p> <p>This sorting order is wrong because I want the following order to appear in the red card section:</p> <ul> <li>11 - Home</li> <li>12 - Home</li> <li>0 - Away</li> <li>11 - Away</li> <li>21 - Away</li> </ul> <p>And in the yellow card section:</p> <ul> <li>1 - Home</li> <li>2 - Home</li> <li>99 - Home</li> <li>1 - Away</li> <li>31 - Away</li> </ul> <p>It looks like the yellow card section sorts correctly but the red card section is showing very weird behavior that makes it appear that it is not getting sorted at all.</p> <p>I am fairly stumped as to why the red card section fails to sort correctly - any thoughts? Should I just sort these objects in memory instead of relying on core data to get my preferred order?</p> <p>Please be aware that this is a core data application with a SQL backed persistence store.</p> <p><strong>UPDATE</strong></p> <p>The following is the SQL statement that core data is using in my fetch:</p> <pre><code>CoreData: annotation: fetch using NSSQLiteStatement &lt;0x11c77cd0&gt; 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 ' </code></pre> <hr>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload