public async Task RefreshAsync(T instance) { if (!await InitializeAsync()) { throw new MobileServiceInvalidOperationException("Unable to refresh data. Initialization failed.", null, null); } await _syncTable.RefreshAsync(instance); }
public async Task Insert_ThenPush_ThenPull_ThenRead_ThenUpdate_ThenRefresh_ThenDelete_ThenLookup_ThenPush_ThenPurge_ThenRead() { ResetDatabase(TestTable); var hijack = new TestHttpHandler(); hijack.AddResponseContent("{\"id\":\"b\",\"String\":\"Hey\"}"); // insert response hijack.AddResponseContent("[{\"id\":\"b\",\"String\":\"Hey\"},{\"id\":\"a\",\"String\":\"World\"}]"); // pull response hijack.AddResponseContent("[]"); // pull last page IMobileServiceClient service = await CreateTodoClient(hijack); IMobileServiceSyncTable <ToDoWithStringId> table = service.GetSyncTable <ToDoWithStringId>(); // first insert an item await table.InsertAsync(new ToDoWithStringId() { Id = "b", String = "Hey" }); // then push it to server await service.SyncContext.PushAsync(); // then pull changes from server await table.PullAsync(null, null); // order the records by id so we can assert them predictably IList <ToDoWithStringId> items = await table.OrderBy(i => i.Id).ToListAsync(); // we should have 2 records Assert.Equal(2, items.Count); // according to ordering a id comes first Assert.Equal("a", items[0].Id); Assert.Equal("World", items[0].String); // then comes b record Assert.Equal("b", items[1].Id); Assert.Equal("Hey", items[1].String); // we made 2 requests, one for push and two for pull Assert.Equal(3, hijack.Requests.Count); // recreating the client from state in the store service = await CreateTodoClient(hijack); table = service.GetSyncTable <ToDoWithStringId>(); // update the second record items[1].String = "Hello"; await table.UpdateAsync(items[1]); // create an empty record with same id as modified record var second = new ToDoWithStringId() { Id = items[1].Id }; // refresh the empty record await table.RefreshAsync(second); // make sure it is same as modified record now Assert.Equal(second.String, items[1].String); // now delete the record await table.DeleteAsync(second); // now try to get the deleted record ToDoWithStringId deleted = await table.LookupAsync(second.Id); // this should be null Assert.Null(deleted); // try to get the non-deleted record ToDoWithStringId first = await table.LookupAsync(items[0].Id); // this should still be there; Assert.NotNull(first); // make sure it is same as Assert.Equal(first.String, items[0].String); // recreating the client from state in the store service = await CreateTodoClient(hijack); table = service.GetSyncTable <ToDoWithStringId>(); await service.SyncContext.PushAsync(); // now purge the remaining records await table.PurgeAsync(); // now read one last time IEnumerable <ToDoWithStringId> remaining = await table.ReadAsync(); // There shouldn't be anything remaining Assert.Empty(remaining); }