public async Task SyncPost(string token) { var realmService = new AsyncRealm <TModel>(); var realmResults = realmService.GetRealmResults(); var items = realmResults.Where(x => !x.Synced).ToList(); var restService = new Services.Rest.Base.Core <TModel>(TargetApi); for (var i = 0; i < items.Count; i++) { if (items[i] == null) { continue; } await UpdateModel(items[i].LocalId); HttpResponseMessage reply; try { reply = await restService.PostAsync(items[i], token); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); continue; } if (reply == null) { continue; } var response = await restService.ParseHttpResponse(reply); if (response.ResultCode == ResultCode.InsertSuccessful || response.ResultCode == ResultCode.UpdateSuccessful) { var model = await restService.ParseResponseItem(reply); var id = model.Id; var itemLocalId = items[i].LocalId; await realmService.WriteAsync(tempRealm => { var localItem = tempRealm.Get(itemLocalId); localItem.Id = id; localItem.Synced = true; }); } } }
protected async Task SyncGet(TModel item) { var realmService = new AsyncRealm <TModel>(); if (item == null) { return; } var itemId = item.Id; TModel localItem; try { localItem = realmService.Get(x => x.Id == itemId); } catch (InvalidOperationException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); localItem = null; } if (localItem != null) // the item exists in the database and matches with the remote item { await realmService.WriteAsync(tempRealm => { var existingItem = tempRealm.Get(x => x.Id == itemId); var mapper = new Utilities.PropertyMapper(); mapper.Map <TModel>(item, existingItem); existingItem.Synced = true; }); } else // the remote item is a new one { item.Synced = true; var localId = 0; await realmService.WriteAsync(tempRealm => { tempRealm.Manage(item); localId = item.LocalId; }); await RebuildModel(localId); } }