我认为我在Xamarin.form和Azure之间的离线更新方面做错了。 我使用Microsoft.Azure.Mobile.Client 4.0.2。所有初始同步工作都很好。我遇到的问题是当我尝试在本地更新项目然后同步到服务器。对于我的测试,我更改Appel(法语程序;-))对象的Statut字段然后调用UpadteAsync(本地更新)然后我调用主syncAppelsAsync(远程更新)
public async Task UpdateAppelAsync(Appel appel)
{
await _appelsTable.UpdateAsync(appel);
await SyncAppelsAsync();
}
然后主要SyncAppelsAsync:
public async Task SyncAppelsAsync()
{
await _authenticationService.SingIn();
ReadOnlyCollection<MobileServiceTableOperationError> syncErrors = null;
try
{
await _appelsTable.PullAsync("allAppels", _appelsTable.CreateQuery());
}
catch (MobileServicePushFailedException exc)
{
if (exc.PushResult != null)
{
syncErrors = exc.PushResult.Errors;
}
}
//Simple error/ conflict handling.
bool reSync = false;
if (syncErrors != null)
{
foreach (var error in syncErrors)
{
if (error.OperationKind == MobileServiceTableOperationKind.Update && error.Result != null)
{
StringBuilder message = new StringBuilder("Champs en problème: Local VS Server" + Environment.NewLine);
List<SyncConflic> conflits = JSonCompare.FindConflicObjects(error.Item, error.Result);
foreach (SyncConflic conflit in conflits)
{
if (conflit.key != "id" && conflit.key != "version" && conflit.key != "updatedAt" & conflit.key != "createdAt")
{
switch (conflit.key)
{
case "statutId":
Statut StatLoc = await this.GetStatutByIdAsync(conflit.local);
Statut StatRem = await this.GetStatutByIdAsync(conflit.remote);
message.Append(conflit.key + ":" + StatLoc.nom + " <> " + StatRem.nom + Environment.NewLine);
break;
default:
message.Append(conflit.key + ":" + conflit.local + " <> " + conflit.remote + Environment.NewLine);
break;
}
}
}
message.Append("Quelle version voulez-vous conserver?" + Environment.NewLine);
if(await _extDialogService.ShowMessage(message.ToString(),"Conflit de MAJ d'un Appel", "Serveur", "Local",null))
{
// Revert to server's copy
await error.CancelAndUpdateItemAsync(error.Result);
}
else
{
// Force local item to server
error.Item["version"] = error.Result["version"];
await error.UpdateOperationAsync(error.Item);
reSync = true;
}
}
else
{
// Discard local change
await error.CancelAndDiscardItemAsync();
ErrorHandling.LogError("Error executing sync operation. Item: " + error.TableName + " (" + error.Item["id"] + "). Operation discarded.");
}
}
}
if(reSync)
{
await SyncAppelsAsync();
}
}
当我调用SyncAppelsAsync时,PullAsync将根据需要执行推送。我遇到的问题是:
要在没有冲突的情况下进行更新,我需要:
我做错了什么?
EDIT2 ------ 我发现2个PATCH操作之间的版本#很奇怪
所以有些东西不会在localDB中回写。在PullAsync完成后......