2017-03-01 88 views
0

我實現了IMobileServiceSyncHandler。我的目標是實施「服務器永遠贏得機制」。因此,當檢測到衝突時,IMobileServiceSyncHandler應該用服務器副本覆蓋本地副本。IMobileServiceSyncHandler - 覆蓋本地版本

這裏是我的代碼:

class MySyncHandler : IMobileServiceSyncHandler 
    { 
     public IMobileServiceSyncTable<Error> localTable; 
     IMobileServiceClient client; 

     public MySyncHandler(IMobileServiceClient client) 
     { 
      this.client = client; 
     } 

     public async Task<JObject> ExecuteTableOperationAsync(IMobileServiceTableOperation operation) 
     { 
      JObject result = null; 
      MobileServicePreconditionFailedException conflictError = null; 
      do 
      { 
       try 
       { 
        result = await operation.ExecuteAsync(); 
       } 
       catch (MobileServicePreconditionFailedException e) 
       { 
        conflictError = e; 


       } 

       if (conflictError != null) 
       { 
        JObject serverItem = conflictError.Value; 

        if (serverItem == null) 
        { 
         serverItem = (JObject)(await operation.Table.LookupAsync((string)operation.Item[MobileServiceSystemColumns.Id])); 
        } 

        await localTable.UpdateAsync(serverItem); 

       } 
      } while (conflictError != null); 

      return result; 
     } 

     public Task OnPushCompleteAsync(MobileServicePushCompletionResult result) 
     { 
      return Task.FromResult(0); 
     } 
    } 

相關部分是:

await localTable.UpdateAsync(serverItem); 

我的想法是與服務器版本更新本地表。我的問題:

這不起作用。本地副本不會更改。它仍然是本地版本。

你能幫忙嗎?

+1

從我看到如果你得到衝突錯誤你永遠不會退出你的do/while循環,因爲conflictError = null;僅在循環外被調用。你能澄清嗎? –

+0

是的,這看起來有點奇怪。但它的微軟和循環的官方例子確實有效。我的問題是,等待localTable.UpdateAsync(serverItem);不起作用 – OPunktSchmidt

+0

「官方樣本」在哪裏?你能提供鏈接嗎?你怎麼知道localTable不會改變?基於用戶界面或你看到的斷點數據?此外,UpdateAsync的重載版本可以提供衝突作爲參數。你嘗試過嗎? –

回答