我实现了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);
我的想法是与服务器版本更新本地表。我的问题:
这不起作用。本地副本不会更改。它仍然是本地版本。
你能帮忙吗?
从我看到如果你得到冲突错误你永远不会退出你的do/while循环,因为conflictError = null;仅在循环外被调用。你能澄清吗? –
是的,这看起来有点奇怪。但它的微软和循环的官方例子确实有效。我的问题是,等待localTable.UpdateAsync(serverItem);不起作用 – OPunktSchmidt
“官方样本”在哪里?你能提供链接吗?你怎么知道localTable不会改变?基于用户界面或你看到的断点数据?此外,UpdateAsync的重载版本可以提供冲突作为参数。你尝试过吗? –