2016-05-26 4 views
7

ich den folgenden Code haben:Play Games Snapshot Konfliktlösung gibt Konflikt

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(); 
    } 
} 

Allerdings hält dieser Code in der while-Schleife stecken zu bleiben. result behält den Status STATUS_SNAPSHOT_CONFLICT. Irgendwelche Ideen, warum das nicht gelöst wird?

Antwort

5

Je nachdem, wie viele Commits zwischen den beiden Versionen stattgefunden haben, müssen Sie möglicherweise mehrere Konflikte in dieser Schleife beheben. Es sollte schließlich stoppen :) Das könnte aber lange dauern.

Weitere Informationen finden Sie unter: 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; 
} 
+0

Der Punkt ist, tut es nicht. –

+0

Wie viele Iterationen hat es getan? Können Sie ein Protokoll teilen? –

+0

Okay, wow. Jetzt hat es getan. Mein Telefon lag gerade über 30 Minuten dort, also vermute ich etwa 100 Iterationen. –

1

Es ist ein Fehler in Google Play-Dienste App offenbar, die mehrere Fixes muss. Bitte beachten Sie diese Diskussion, wo Google beteiligt ist: GitHub discussion and fix info