Reputation: 2355
I've got the following code:
Snapshots.OpenSnapshotResult result;
result = Games.Snapshots.open(googleApiClient, "save", true).await();
while (result == null || !result.getStatus().isSuccess()) {
Log.d("Snapshot", "Open snapshot");
if (result.getStatus().getStatusCode() == GamesStatusCodes.STATUS_SNAPSHOT_CONFLICT) {
Snapshot snapshot = result.getSnapshot();
Snapshot conflictSnapshot = result.getConflictingSnapshot();
// Resolve between conflicts by selecting the newest of the conflicting snapshots.
Snapshot mResolvedSnapshot = snapshot;
if (snapshot.getMetadata().getLastModifiedTimestamp() <
conflictSnapshot.getMetadata().getLastModifiedTimestamp()) {
mResolvedSnapshot = conflictSnapshot;
}
result = Games.Snapshots.resolveConflict(
googleApiClient, result.getConflictId(), mResolvedSnapshot).await();
}
}
However, this code keeps getting stuck in the while loop. result
keeps having the status STATUS_SNAPSHOT_CONFLICT
. Any ideas as to why this is not getting resolved?
Upvotes: 7
Views: 1264
Reputation: 633
There's a bug in Google Play Services app apparently which needs multiple fixes. Please see this discussion where Google is involved: GitHub discussion and fix info
Upvotes: 1
Reputation: 4572
Depending on how many commits have happened between the two versions, you may need to resolve multiple conflicts in that loop. It should eventually stop :) This might take seriously long though.
For more details see: https://developers.google.com/games/services/android/savedgames#handling_saved_game_conflicts
// Some large number to be defensive against an infinite loop.
static final int MAX_SNAPSHOT_RESOLVE_RETRIES = 100;
Snapshots.OpenSnapshotResult result;
result = Games.Snapshots.open(googleApiClient, "save", true).await();
Snapshot snapshot = processSnapshotOpenResult(result, int retryCount);
Snapshot processSnapshotOpenResult(Snapshots.OpenSnapshotResult result, int retryCount) {
Snapshot mResolvedSnapshot = null;
retryCount++;
int status = result.getStatus().getStatusCode();
Log.i(TAG, "Save Result status: " + status);
if (status == GamesStatusCodes.STATUS_OK) {
return result.getSnapshot();
} else if (status == GamesStatusCodes.STATUS_SNAPSHOT_CONTENTS_UNAVAILABLE) {
return result.getSnapshot();
} else if (status == GamesStatusCodes.STATUS_SNAPSHOT_CONFLICT) {
Snapshot snapshot = result.getSnapshot();
Snapshot conflictSnapshot = result.getConflictingSnapshot();
// Resolve between conflicts by selecting the newest of the conflicting snapshots.
mResolvedSnapshot = snapshot;
if (snapshot.getMetadata().getLastModifiedTimestamp() <
conflictSnapshot.getMetadata().getLastModifiedTimestamp()) {
mResolvedSnapshot = conflictSnapshot;
}
Snapshots.OpenSnapshotResult resolveResult = Games.Snapshots.resolveConflict(
mGoogleApiClient, result.getConflictId(), mResolvedSnapshot).await();
if (retryCount < MAX_SNAPSHOT_RESOLVE_RETRIES) {
// Recursively attempt again
return processSnapshotOpenResult(resolveResult, retryCount);
} else {
// Failed, log error and show Toast to the user
String message = "Could not resolve snapshot conflicts";
Log.e(TAG, message);
Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
}
}
// Fail, return null.
return null;
}
Upvotes: 5