private async Task SyncUp(SyncState sync, Action <SyncState> callback) { if (sync == null) { throw new SmartStoreException("SyncState sync was null"); } var target = (SyncUpTarget)sync.Target; HashSet <string> dirtyRecordIds = GetDirtyRecordIds(sync.SoupName, SmartStore.Store.SmartStore.SoupEntryId); int totalSize = dirtyRecordIds.Count; sync.TotalSize = totalSize; const int i = 0; foreach ( JObject record in dirtyRecordIds.Select( id => _smartStore.Retrieve(sync.SoupName, long.Parse(id))[0].ToObject <JObject>())) { await SyncUpOneRecord(target, sync.SoupName, sync.Options.FieldList, record, sync.MergeMode); int progress = (i + 1) * 100 / totalSize; if (progress < 100) { UpdateSync(sync, SyncState.SyncStatusTypes.Running, progress, callback); } } }
/// <summary> /// Build SyncState from store sync given by id /// </summary> /// <param name="store"></param> /// <param name="id"></param> /// <returns></returns> public static SyncState ById(SmartStore.Store.SmartStore store, long id) { JArray syncs = store.Retrieve(Constants.SyncsSoup, id); if (syncs == null || syncs.Count == 0) { return null; } return FromJson(syncs[0] as JObject); }
private async Task SyncUp(SyncState sync, Action <SyncState> callback, CancellationToken token = default(CancellationToken), Action <string, string> idChangeHandler = default(Action <string, string>)) { if (sync == null) { throw new SmartStoreException("SyncState sync was null"); } var target = (SyncUpTarget)sync.Target; try { var account = AccountManager.GetAccount(); if (account != null) { await OAuth2.CallIdentityService(account.IdentityUrl, SyncManager.GetInstance().RestClient); } } catch (Exception e) { PlatformAdapter.SendToCustomLogger(e, LoggingLevel.Error); if (!string.IsNullOrEmpty(e.Message) && (e.Message.ToUpper().Contains("FORBIDDEN") || e.Message.Contains("403"))) { throw new OAuthException("Token Expired"); } } HashSet <string> dirtyRecordIds = GetDirtyRecordIds(sync.SoupName, SmartStore.Store.SmartStore.SoupEntryId); int totalSize = dirtyRecordIds.Count; sync.TotalSize = totalSize; int i = 0; foreach ( JObject record in dirtyRecordIds.Select( id => _smartStore.Retrieve(sync.SoupName, long.Parse(id))[0].ToObject <JObject>())) { if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); } List <string> fields = new List <string>(sync.Options.FieldList); if (record.ExtractValue <bool>(LocallyDeleted)) { } else if (record.ExtractValue <bool>(LocallyCreated)) { } else if (record.ExtractValue <bool>(LocallyUpdated)) { foreach (string value in sync.Options.fieldsToExcludeOnUpdate) { if (fields.Contains(value)) { fields.Remove(value); } } } bool res = await SyncUpOneRecord(target, sync.SoupName, fields, record, sync.MergeMode, idChangeHandler); if (res) { i++; } int progress = i * 100 / totalSize; if (progress < 100) { UpdateSync(sync, SyncState.SyncStatusTypes.Running, progress, callback); } } if (i < totalSize) { throw new SmartStoreException("Cannot sync all records"); } }