Search code examples
androidazurerequestresponseazure-mobile-services

Eventual problems getting response in mobile clients after doing insert from azure mobile service


I have an app that runs on android mobile smartphones and connects to azure mobile service backend (based on node js). Basically what I do is insert data to a table in the mobile service database. In the mobile app, after inserting my object I check if the response object has an id different to null, in that case I delete the row in the local sqlite database, if id equals "null" then I retry the operation later. The problem is that frequently I don't receive the id, so that id="null" and I retry operation, nevertheless when fetching the azure database the row was successfully inserted.

Here is part of my code:

    if (downeventstable == null && !error)
    downeventstable = FirstmenuScreen.mClient.getTable("downevents" + servicio, downevents.class); 

boolean error2 = true;

try {
    downeventstable.insert(dwnevent).get();
    error2=false;
        } catch (Exception e) {                   
                 error2 = true;
                  break; 
                    }


if (!error2 && dwnevent.getId() != null) {
                        // if no error and id was received deletes row locally in sqlite
                    }

I guess that the problem is caused by a bad quality of the mobile network, maybe the app lost connection after a successfull insertion in azure and didn't get the response object. How may I know if the data was inserted in azure and treat these situations?


Solution

  • If your insert was successful and you try to re-insert the same record with the same ID later, you'll get a 409 conflict. You can handle this in your sync conflict handler if you're using offline sync, or just exception handling if you're using a regular table.

    Here's a Xamarin sample that handles the 409 conflict, which is MobileServiceConflictException in C#. You would do something similar in Java. You can test this out by using a REST client and seeing that you get a 409.

    Btw, it sounds like you are doing a lot of manual work with your own SQLite tables. You might want to consider using the offline sync feature that is built in, which will do the retries for you: Add Offline Data Sync to your Android Mobile Services app.

    Note that Mobile Services is being deprecated, so we recommend that you upgrade your code to Mobile Apps if it is a new app. See Announcing Azure Mobile Services transition to Azure App Service.