Fivos
Fivos

Reputation: 568

Azure Mobile Services Android Offline Example - Null Id when inserting in SQLiteLocalStore

When i "add" a new toDoItem in the list, the item does not get an Id (null) from the local database at that time. So if you try to edit that item, you will get an error. On the other hand if you add a new toDoItem, and then "refresh" the list or edit another item, the new item that you added gets an Id. The example i followed is here: http://azure.microsoft.com/blog/2014/08/07/offline-support-in-azure-mobile-services-android-sdk/ What could be wrong?

I could create a random ID and pass it in each ToDoItem when i press "Add" button, but i want the database to do it for me at that time.

Thank you in advance!

Upvotes: 0

Views: 597

Answers (1)

Naren
Naren

Reputation: 877

The error could be causing because of the following reasons.

  1. There is a bug in the addItem function of the blog. When you insert an item in the local database it actually does assign an id to it. When you call the insert function on the table it returns an object with an id.

    So insert for todoItem should look like this.

    final ToDoItem entity = mToDoTable.insert(item).get();

The correct version of the addItem function looks like this

public void addItem(View view) {
            if (mClient == null) {
                return;
            }

            // Create a new item
            final ToDoItem item = new ToDoItem();
            item.setText(mTextNewToDo.getText().toString());
            item.setComplete(false);

            // Insert the new item
            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    try {
                        final ToDoItem entity = mToDoTable.insert(item).get();
                        if (!entity.isComplete()) {
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    mAdapter.add(entity);
                                }
                            });
                        }
                    } catch (Exception exception) {
                        createAndShowDialog(exception, "Error");
                    }
                    return null;
                }
            }.execute();
            mTextNewToDo.setText("");
        }
  1. There is a bug Android SDK (https://github.com/Azure/azure-mobile-services/issues/505). According to which your Mobile Service and the client table definitions should match. When you create the todoItem table it adds the "__deleted" column by default. You can either define the column in your client or remove it from todoItem table.

Upvotes: 1

Related Questions