Azure移动客户端:冲突处理UPDATE操作error.Result == null

时间:2017-03-02 07:35:10

标签: azure-mobile-services

在我的冲突处理代码中,如果更新操作,error.Resultnull,我就不知道原因或原因导致

那是我的代码:

 private async Task HandleConflictResolutionOfLocalTablePushAttemptAsync(ReadOnlyCollection<MobileServiceTableOperationError> syncErrors)
        {
            /*
             * * Handling Conflict Resolution

             * * see :https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter3/client/#handling-conflict-resolution
             * * */


            // Simple error/conflict handling. A real application would handle the various errors like network conditions,
            // server conflicts and others via the IMobileServiceSyncHandler.
            if (syncErrors != null)
            {
                foreach (var error in syncErrors)
                {
                    var localItem = error.Item;
                    var serverItem = error.Result;

                    if (error.OperationKind == MobileServiceTableOperationKind.Update && error.Result != null)
                    {
                        var localVersion = localItem?[MobileServiceSystemColumns.Version];
                        var serverVersion = serverItem?[MobileServiceSystemColumns.Version];

                        if (localVersion == null)
                        {
                            if (await IsRemoteBackendReachable())
                            {
                                //Update failed, reverting to server's copy.
                                await error.CancelAndUpdateItemAsync(serverItem);

                                await MetricsManagerHelper.Instance
                                    .SendErrorToApplicationInsightsAsync($"Operation: {error.OperationKind} LocalVersion == NULL." +
                                                                         "Action: Reverting to remote-db server's copy\r\n" +
                                                                         $"Table: {error.TableName} Record Id: {localItem?[MobileServiceSystemColumns.Id]}\r\n" +
                                                                         $"Local: {localItem}\r\n" +
                                                                         $"Server: {serverItem}");
                            }

                            continue;
                        }


                        if (serverVersion == null)
                        {
                            if (await IsRemoteBackendReachable())
                            {
                                //Update failed, reverting to server's copy.
                                await error.CancelAndUpdateItemAsync(serverItem);

                                await MetricsManagerHelper.Instance
                                    .SendErrorToApplicationInsightsAsync($"Operation: {error.OperationKind} ServerVersion == NULL.\r\n" +
                                                                         "Action: Reverting to remote-db server's copy\r\n" +
                                                                         $"Table: {error.TableName} Record Id: {localItem?[MobileServiceSystemColumns.Id]}\r\n" +
                                                                         $"Local: {localItem}\r\n" +
                                                                         $"Server: {serverItem}");
                            }

                            continue;
                        }


                        // Set Local version to Server Version an Update again. PendingOpertation is set to new updated localItem and in outer loop a PushAsync is attempted again.
                        localItem[MobileServiceSystemColumns.Version] = serverItem[MobileServiceSystemColumns.Version];

                        if (await IsRemoteBackendReachable())
                        {
                            await error.UpdateOperationAsync(JObject.FromObject(localItem));
                            await MetricsManagerHelper.Instance.SendInfoToApplicationInsightsAsync($"Operation: {error.OperationKind} LocalVersion older than (<) ServerVersion. LocalItem updated in local.db.");
                        }
                    }
                    else
                    {
                        if (await IsRemoteBackendReachable())
                        {
                            // Discard local change.
                            await error.CancelAndDiscardItemAsync();

                            await MetricsManagerHelper.Instance
                                .SendErrorToApplicationInsightsAsync($"Operation: {error.OperationKind} PUSH failed.\r\n" +
                                                                     "Action: Discard local-db change.\r\n" +
                                                                     $"Table: {error.TableName} Record Id: {localItem?[MobileServiceSystemColumns.Id]}\r\n" +
                                                                     $"Local: {localItem}\r\n" +
                                                                     $"Server: {serverItem}");
                        }
                    }
                }
            }
        }

如果error.Resultnull,我除了丢弃localItem更改之外别无选择。

如果error.Result null 2017-03-01T12:10:01.889-0800 I STORAGE [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating 可能发生,你能解释一下吗? 我唯一合理的猜测是,intenet连接丢失,无法检索到serverItem。但是在我调用冲突处理程序之前,我立即检查我的远程后端是否可以访问。

提前致谢

埃里克

0 个答案:

没有答案