我有以下代码:
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();
}
}
然而,这段代码一直停留在while循环中。 result
保持状态STATUS_SNAPSHOT_CONFLICT
。关于为什么没有得到解决的任何想法?
答案 0 :(得分:5)
根据两个版本之间发生的提交次数,您可能需要解决该循环中的多个冲突。它应该最终停止:)这可能需要很长时间。
有关详细信息,请参阅: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;
}
答案 1 :(得分:1)
Google Play服务应用中存在一个显然需要多个修复程序的错误。请参阅Google参与的讨论: GitHub discussion and fix info