Search code examples
androidandroid-contentprovider

Do I need to use the same URI returned from my insert() method to fetch a row


I have a simple doubt regarding Android Content providers .

While inserting a single record into db I am returning a URI by appending the _id fetched from the insert() method and also I am calling getContentResolver().notifyChange() on this URI .

So now my URI will be "content://CONTENT_AUTHORITY/TABLE_NAME/_id".

But while fetching a single record , I am fetching it by selecting another id (a composite key) let it be movie_id .

So to fetch this my uri will be "content://CONTENT_AUTHORITY/TABLE_NAME/movie_id"

Now i will be able to fetch a record since my URI matcher matches only /TABLE_NAME/# records.

But what happens to the first URI which was created, on which I had created the notifyChange() method?

Is it bad practice to do so, since I am using notifyChange() on one URI but I am fetching using other URI?


Solution

  • The best practice is indeed that the uri you return in the insert() method can be used to query the exact same object. Obvisouly it is easier if you only have one uri format, and some workflow might be extremely usefull.

    But then again, there's no issue with having alias uris too, and be able to query a row from a table based on different unique ids. But if you do that, please use a different URI, so that your content provider knows which query to perform.

    In your example, I'd let the content://CONTENT_AUTHORITY/TABLE_NAME/_id query entries by the row id, and add a content://CONTENT_AUTHORITY/TABLE_NAME/MOVIE/movie_id uri to query entries by composite key