IMobileServiceSyncHandler - 覆盖本地版本

IMobileServiceSyncHandler - Override local version

我实现了一个 IMobileServiceSyncHandler。我的目标是实现一个"server always wins mechanism"。因此,当检测到冲突时,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);

我的想法是用服务器版本更新本地table

我的问题:

这不起作用。本地副本不会更改。它仍然是本地版本。

你能帮忙吗?

同一位工程师在这里有一个更完整的例子:Azure Mobile Services - Handling Conflicts with Offline

为了保持服务器版本的记录,将这一行替换为:

await localTable.UpdateAsync(serverItem);

return serverItem;

出现冲突时在 if 块内。