I am applying a batch of ContentProviderOperations on my provider:
ContentProviderResult[] result = resolver.applyBatch(...)
Everything works as expected the data is being inserted into the DB, but if I want to extract the id(s) the last element of the uri which should be the id is always null.
Is this happening because I have set the _id of the table to autoincrement (in other words would it work if I am not autoincrementing the id and fill it with a manually uid from my code).
If not, can anyone tell me whats causing this behavior.
Update: This is the String for creating the table:
private static final String CREATE_TABLE_WORKFLOWSTATES =
"CREATE TABLE " + Tables.WORKFLOWSTATES + " ("
+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ WorkflowStatesColumns.NAME + " TEXT NOT NULL,"
+ "UNIQUE ("+ WorkflowStatesColumns.NAME + ") ON CONFLICT IGNORE)";
and if i view the table I can see the columns _id and name, the inserted data that is visible shows that the autoincrement works properly.
Update 2: This is how I have build the ContentProviderOperation:
this is my ArrayList with ContentProviderOperations (CollectionUtils is a custom class in order to instantiate the Arraylist):
ArrayList<ContentProviderOperation> batch = CollectionUtils.newArrayList();
and this is the actual operation:
batch.add(ContentProviderOperation
.newInsert(InvoiceContract.addCallerIsSyncAdapterParameter(WorkflowStates.CONTENT_URI))
.withValue(WorkflowStates.NAME, task.getWFS()).build());
In case of insertion (and I guess that you insert a row into your database), the
result[i].uricontains URI of a newly inserted row, where i is the index of the corresponding operation in operations array. Try to print this URI and you’ll see if it corresponds to real id.If one of your operation is UPDATE or DELETE the corresponding result URI will be null, but
result[i].countwill contain the number of updated/deleted rows.UPDATE
I guess the problem is that you have a unique index on NAME. Maybe, you have a row already inserted with the same name?
UPDATE2
The problem was that the insert function in the ContentProvider did not return the id correctly.