How to sort GKTurnBasedMatch recently active?

I create a simple pun with a menu screen in which I show all active user matches. I would like to sort this array of matches in order from the last time to the last moment, but the only timestamp property associated with players with a rotation is the GKTurnBasedParticipant ... GKTurnBasedMatch does not have a useful sorting property.

GKTurnBasedMatch has an array of GKTurnBasedParticipant objects as a property, so I can certainly come up with some solution, but I can’t think of anything that wouldn’t be really dirty and inefficient, Is there a way that it could be somehow simple In this case, NSPredicate use NSPredicate to expand into each array of participants, see the last timestamp and sort all matches in one go?

+4
source share
2 answers

I don’t have a solution based on NSPredicate or perhaps as elegant as you hoped for, but I ran into the same problem and wrote my own solution, and in fact it wasn’t.

My solution is for a game in which there can only be two participants, so modify them accordingly, but here is the code that I ended up using:

 [myGamesArray sortUsingComparator:^NSComparisonResult(CHGame *game1, CHGame *game2) { if (YES == [game1 localPlayersTurn] && NO == [game2 localPlayersTurn]) { return NSOrderedAscending; } else if (NO == [game1 localPlayersTurn] && YES == [game2 localPlayersTurn]) { return NSOrderedDescending; } NSDate *lm1 = [game1.match lastMove]; NSDate *lm2 = [game2.match lastMove]; if (lm1 != nil && lm2 != nil) { return [lm1 compare:lm2]; } return NSOrderedSame; }]; 

where CHGame is a custom class that I built for my games (which have the GKTurnBasedMatch match property), and the localPlayersTurn instance localPlayersTurn returns a BOOL indicating whether it is a local member rotate or not.

And then I wrote the lastMove method in the category on GKTurnBasedMatch :

 - (NSDate *)lastMove { GKTurnBasedParticipant *localParticipant, *otherParticipant; NSDate *lastMove; for (GKTurnBasedParticipant *participant in self.participants) { if (YES == [participant.playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) { localParticipant = participant; } else { otherParticipant = participant; } } if (localParticipant == self.currentParticipant) { lastMove = otherParticipant.lastTurnDate; } else { lastMove = localParticipant.lastTurnDate; } return lastMove; } 

Again, this only works for two participants, but they are easy to change for any number.

Hope this helps, although this is not quite what you asked for.

+3
source

Sort step-by-step matches using the last turn of the current member

 [GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error) { NSString *descriptorKey = @"currentParticipant.lastTurnDate"; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:descriptorKey ascending:NO]; NSArray *sortedMatches = [matches sortedArrayUsingDescriptors:@[sortDescriptor]]; }]; 



Sort incremental matches by date created

 [GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error) { NSString *descriptorKey = @"creationDate"; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:descriptorKey ascending:NO]; NSArray *sortedMatches = [matches sortedArrayUsingDescriptors:@[sortDescriptor]]; }]; 
0
source

All Articles