private async Task <IMobileServiceSyncTable <T> > GetInitialisedSyncTable <T>(string key, Expression <Func <T, bool> > query = null) { MobileServicePushFailedException pushFailedException = null; var table = mobileService.GetSyncTable <T>(); var mobileServicesQuery = query == null ? null : table.Where(query); try { if (internetAccessIsAvailable) { await table.PullAsync(key, mobileServicesQuery ?? table.CreateQuery()); } } catch (MobileServicePushFailedException ex) { pushFailedException = ex; } if (pushFailedException != null) { string errorMessages = pushFailedException.PushResult .Errors .Aggregate(new StringBuilder(), (s, a) => s.AppendLine(a.RawResult), s => s.ToString()); await logger.LogExceptionAsync(pushFailedException, Constants.LOGGING_DATAKEY_PUSHERRORS, errorMessages); throw pushFailedException; } return(table); }
/// <summary> /// Performs Simple Conflict Resolution reverting to server copy if mobile api update fails /// </summary> /// <param name="mspfe">MobileServicePushFailedException occured during sync</param> /// <returns></returns> private async Task SimpleConflictResolution(MobileServicePushFailedException mspfe) { if (mspfe.PushResult != null) { foreach (var error in mspfe.PushResult.Errors) { if (error.OperationKind == MobileServiceTableOperationKind.Update && error.Result != null) { //Update failed, reverting to server's copy. await error.CancelAndUpdateItemAsync(error.Result); } if (error.OperationKind == MobileServiceTableOperationKind.Insert) { await error.CancelAndUpdateItemAsync(error.Result); } else { // Discard local change. await error.CancelAndDiscardItemAsync(); } Debug.WriteLine(@"Error executing sync operation. Item: {0} ({1}). Operation discarded.", error.TableName, error.Item["id"]); } } }
private async Task PerformOperationAndRetryIfUnauthorized(Func <Task> operation) { MobileServicePushFailedException pushFailedException = null; try { await operation(); } catch (MobileServiceInvalidOperationException ex) { if (ex.Response.StatusCode != HttpStatusCode.Unauthorized) { throw; } AccessToken = null; } catch (MobileServicePushFailedException ex) { pushFailedException = ex; } if (pushFailedException != null) { string errorMessages = pushFailedException.PushResult .Errors .Aggregate(new StringBuilder(), (s, a) => s.AppendLine(a.RawResult), s => s.ToString()); await logger.LogExceptionAsync(pushFailedException, Constants.LOGGING_DATAKEY_PUSHERRORS, errorMessages); throw pushFailedException; } var accessTokenUpdated = await AccessTokenCouldBeMadeCurrent(); if (accessTokenUpdated) { await operation(); } else { throw new UnauthorizedAccessException(); } }