我有以下代码:

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 .关于为什么这没有得到解决的任何想法?

请您参考如下方法:

根据两个版本之间发生的提交次数,您可能需要解决该循环中的多个冲突。应该最终停止 :) 这可能需要很长时间。

更多详情请见: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; 
} 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!