Last active
August 29, 2015 13:58
-
-
Save prime31/10012039 to your computer and use it in GitHub Desktop.
One example (of many, many examples) that shows the difference in API design between Apple and Google. This example shows the code required to load a list of turn based matches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Apple API for loading all your turn-based matches | |
[GKTurnBasedMatch loadMatchesWithCompletionHandler:^( NSArray *matches, NSError *error ) | |
{ | |
// there is either an error or a result. nice and simple. | |
if( error ) | |
log( "print a useful error: " + error ); | |
else | |
log( "the matches are here: " + matches ); | |
}]; | |
// Google API for loading all your turn-based matches on Android | |
int[] statuses = new int[] { TurnBasedMatch.MATCH_TURN_STATUS_MY_TURN, TurnBasedMatch.MATCH_TURN_STATUS_THEIR_TURN, TurnBasedMatch.MATCH_TURN_STATUS_COMPLETE }; | |
Games.TurnBasedMultiplayer.loadMatchesByStatus( helper.getApiClient(), statuses ) | |
.setResultCallback( new ResultCallback<LoadMatchesResult>() | |
{ | |
@Override | |
public void onResult( LoadMatchesResult result ) | |
{ | |
if( result.getStatus().getStatusCode() != GamesStatusCodes.STATUS_OK ) | |
{ | |
log( "we don't have a useful error but we have an integar that we have to look up in the documentation: " + result.getStatus().getStatusCode() ); | |
} | |
else | |
{ | |
// so, we just wanted a List of matches but Google would never make it that simple. We'll create the | |
// matches List and fill it ourselves | |
List<TurnBasedMatch> matches = new ArrayList<TurnBasedMatch>(); | |
// what do we get from Google instead? a LoadMatchesResponse (result.getMatches returns it) of course! | |
// and what is inside a LoadMatchesResponse? three TurnBasedMatchBuffer objects! to get those matches we | |
// asked for we have to loop through each of the three TurnBasedMatchBuffers and extract the matches | |
// one-by-one. | |
for( int i = 0; i < result.getMatches().getCompletedMatches().getCount(); i++ ) | |
matches.add( result.getMatches().getCompletedMatches().get( i ) ); | |
for( int i = 0; i < result.getMatches().getMyTurnMatches().getCount(); i++ ) | |
matches.add( result.getMatches().getMyTurnMatches().get( i ) ); | |
for( int i = 0; i < result.getMatches().getTheirTurnMatches().getCount(); i++ ) | |
matches.add( result.getMatches().getTheirTurnMatches().get( i ) ); | |
log( "yay! we finally got what we asked for! matches: " + matches ); | |
} | |
} | |
}); | |
// oddly enough, the iOS Google API is more to the point though still you have to search deep in the documentation to | |
// figure out that to load turn-based matches you use the applicationModel and call reloadDataForKey with the magic | |
// string GPGModelAllMatchesKey. why not just expose a loadMatches method on a relevant class such as GPGTurnBasedMatch? | |
[[GPGManager sharedInstance].applicationModel reloadDataForKey:GPGModelAllMatchesKey | |
completionHandler:^( NSError *error ) | |
{ | |
// there is either an error or we can fetch the results | |
if( error ) | |
log( "print a useful error: " + error ); | |
else | |
log( "the matches are here: " + [GPGManager sharedInstance].applicationModel.turnBased.allMatches ); | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment