Rodney Littles
Rodney Littles

Reputation: 564

Azure Mobile Services - Resolving offline Insert with online

I have an Offline Sync Table that I insert an item into. My Mobile Services Web Api controller adds and returns a different Id than the one that is inserted into the local SQLite database using IMobileServiceSyncTable<T>.InsertAsync(item) (expected behavior).

The post to the server is fine. The item gets created by the API. The problem I have is with the resolution of the item locally. I am following up this operation with a PullAsync() operation. When I look at my list of items, the original item I created is present with a guid for an Id (expected behavior), and the item created through the API is present with my database Id. I need to merge the API returned item with local item.

I am not sure if moving this to an online insert would solve my problem. I have another operation that is immediately dependent on this operations success. The follow up operation fails miserably because, obviously, the guid Id value isn't in found in my API and I get a string of HTTP 500 errors.

I am wondering if there is a best practice around resolving this, or something I am not aware of that will easily fix this problem. I would assume this is common for people using the framework? The id's in the local SQLite store will most likely never match the actual database values behind an API.

I have read through the following (and more around the topic) and I am still unclear on how to resolve this issue.

Any advice is welcome.

Upvotes: 0

Views: 336

Answers (1)

Bruce Chen
Bruce Chen

Reputation: 18465

The sync framework is pushing all queued operations to the server, i.e. pushing the insert to the api endpoint. The guid id is inserted in the local table, and then immediately pushed to the server. When it returns from the server I do a sync (making sure that I get any updates from the server). After the pull, the local table has two items. The one inserted locally, and the one that was pushed to the API endpoint with a "valid" Id.

For checking this issue, I selected Node.js as my backend language without downloading the c# server project then deploy it to azure mobile app. Based on your description, I inserted a new item into my local table, then push all pending local operations against the remote table, then pull the latest items from the remote table. Here is the code snippet and network traces captured by fiddler:

var todoItem = new TodoItem { Text = TextInput.Text };
//insert a new item into local table
await todoTable.InsertAsync(todoItem);
//push all pending local operations against the remote table
await App.MobileService.SyncContext.PushAsync();  //offline sync
await todoTable.PullAsync("todoItems", todoTable.CreateQuery());

Network trace for PushAsync:

enter image description here

Note: Since your local item has the GUID value for the id property, during an insert against the remote table, the server would not generate the new value, if the id value exists then the 409 conflict would be returned.

Network trace for PullAsync:

enter image description here

I would recommend you capture the network traces to narrow this issue. And it seems that your push operation did not work as expected, you need to check the id value from the response of your push operation. If you built the c# backend, I assume that you need to check your table model, more details your could refer to adrian hall's book here. Also, you could refer to the mobile apps quickstart sample for .NET backend here.

Upvotes: 1

Related Questions