Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Games Snapshot conflict resolution gives conflict

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?

like image 335
Daniël van den Berg Avatar asked May 26 '16 17:05

Daniël van den Berg


2 Answers

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;
}
like image 139
Clayton Wilkinson Avatar answered Oct 20 '22 06:10

Clayton Wilkinson


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

like image 26
Maria Avatar answered Oct 20 '22 04:10

Maria