0
votes

I have an android app that comunicate with azure sql database. I'm implementing offline sync but after the first launch of the app the local tables don't sync with the database and viceversa. Only the first time i have the pull to the local tables.

private AsyncTask<Void, Void, Void> initLocalStore() throws MobileServiceLocalStoreException, ExecutionException, InterruptedException {

AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        try {

            MobileServiceSyncContext syncContext = mClient.getSyncContext();

            if (syncContext.isInitialized())
                return null;

            SQLiteLocalStore localStore = new SQLiteLocalStore(mClient.getContext(), "DB_ONYAX", null, 1); //DB_ONYAX = database name

            //Level_Onyax_Structures table definition
            Map<String, ColumnDataType> tableDefinitionLOS = new HashMap<String, ColumnDataType>();
            tableDefinitionLOS.put("id", ColumnDataType.String);
            tableDefinitionLOS.put("updatedAt", ColumnDataType.DateTimeOffset);
            tableDefinitionLOS.put("version", ColumnDataType.String);
            tableDefinitionLOS.put("ID_ONYAX_STRUCTURE", ColumnDataType.Integer);
            tableDefinitionLOS.put("Structure_name", ColumnDataType.String);
            tableDefinitionLOS.put("Structure_LAT", ColumnDataType.Real);
            tableDefinitionLOS.put("Structure_LON", ColumnDataType.Real);
            tableDefinitionLOS.put("Structure_ZOOM", ColumnDataType.Real);

            localStore.defineTable("Level_Onyax_Structures", tableDefinitionLOS);
           SimpleSyncHandler handler = new SimpleSyncHandler();

            syncContext.initialize(localStore, handler).get();

        } catch (final Exception e) {
            createAndShowDialogFromTask(e, "Error Creation Local Table");
        }

        return null;
    }
};

return runAsyncTask(task);
}

Refresh the items from the mobile service:

 private List<My_Table> refreshItemsFromMobileServiceTableSyncTable() throws ExecutionException, InterruptedException {
    //sync the data
    syncLOS().get();
    return mLOSTable.read(null).get();
}

Sync method:

private AsyncTask<Void, Void, Void> syncLOS() {
    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
        @Override
        protected Void doInBackground(Void... params) {
            try {
                MobileServiceSyncContext syncContext = mClient.getSyncContext();
                syncContext.push().get();
                mLOSTable.pull(null).get();
            } catch (final Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    return runAsyncTask(task);
}

And here I call everything:

AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
        @Override
        protected Void doInBackground(Void... params) {

            try {
                listOnyaxStructures = refreshItemsFromMobileServiceTableSyncTable();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        setLoading(false);

                        }
                    }
                });
            } catch (final Exception e){
                e.printStackTrace();
            }

            return null;
        }
    };

    runAsyncTask(task);

Where I try to update a value:

item.setStructure_name(edt.getText().toString());

                new AsyncTask<Void, Void, Void>(){
                    @Override
                    protected Void doInBackground(Void... params) {
                        try {

                            mLCSTable.update(item).get();
                            MobileServiceSyncContext syncContext = mClient.getSyncContext();
                            syncContext.push().get();
                            mLCSTable.pull.get();

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //finish
                                }
                            });
                        } catch (final Exception e) {
                            e.printStackTrace();
                        }

                        return null;
                    }
                }.execute();

Any helps?

1
When i update the item, it's updated only on the local storage. - Aleksandar Boshnakov

1 Answers

0
votes

UPDATE: I found the cause of MobileServicePushFailedException. I got MobileServicePushFailedException because when I try to update an item I'm trying to update an index that is autoincrement.

This is the error from Easy Tables's log: Cannot update identity column 'ID_COMPANY_STRUCTURE'.

I've already tried not to use ID_COMPANY_STRUCTURE by not inserting it in the local table and everything work well, but I need it because it is a foreign key to another table.

Is there a way to SET IDENTITY OFF on android or a way to avoid this type of error?