4

I am new to android can any one tell me what is the difference between cursor.setNotificationUri() and getContentResolver().notifyChange(uri,null) while implementing content provider.

I have seen that cursor.setNotificationUri() is used in query() method and while updating or inserting getContentResolver().notifyChange() is used.

I have little understanding that getContentResolver().notifyChange() notifies resolver that some data has been changed but what does cursor.setNotificationUri() do there ?

Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
FaisalAhmed
  • 3,469
  • 7
  • 46
  • 76
  • 1
    Possible duplicate of [What is cursor.setNotificationUri() used for?](http://stackoverflow.com/questions/21623714/what-is-cursor-setnotificationuri-used-for) – XGouchet Nov 08 '16 at 07:23

1 Answers1

15

They are used symbiotically. If you are implementing a ContentProvider, essentially when someone queries your provider, you produce a Cursor and call setNotificationUri() on it with some rational Uri (such as the Uri used to make the query). Later, if data served by your ContentProvider changes, e.g. after an insert/update/delete, you call getContentResolver().notifyChange(uri, null) so that anyone who currently has a Cursor (because they queried earlier) gets notified that data has changed and they should re-query. If they are using a CursorLoader, the re-query happens automatically.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • I noticed one thing, if you don't use `setNotificationUri()` in `query()` method and `notifyChange()` in update/delete/insert methods, your Content Provider will still work the same. – CopsOnRoad Jan 24 '18 at 07:42