ContentObserver is called even when there are no changes in the cursor

I have a content observer that needs to be notified when one of the contacts added by my sync adapter changes. I register and unregister the observer doing this:

private static final Uri MYAPP_CONTENT_URI = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(RawContacts.ACCOUNT_NAME, SyncAdapter.MYAPP_ACCOUNT_NAME).appendQueryParameter(RawContacts.ACCOUNT_TYPE, MY_APP_ACCOUNT_TYPE).build(); public static void registerContentObserver() { ContentResolver resolver = MyApplication.getAppContext().getContentResolver(); cursorContacts = resolver.query(MYAPP_CONTENT_URI, null, RawContacts.DELETED + "=0", null, null); cursorContacts.registerContentObserver(MYAPP_URI_OBSERVER); } public static void unregisterContentObserver() { if (cursorContacts != null) { cursorContacts.unregisterContentObserver(MYAPP_URI_OBSERVER); cursorContacts.close(); } } 

The problem is that even when the cursor is empty (getCount returns 0) after registering the observer, I get an onChange call, which I do in my own address book. Should you not call an observer only when one of the entries in the cursor has been changed? The documentation states:

Register an observer that is called when changes occur with content that supports this cursor.

What is content that supports this cursor? I thought this was a list of lookupuri contacts in the cursor, but it seems like this is enough to have changes in ContactsContract.RawContacts.CONTENT_URI.

I also tried to register one observer for each Uri. It does not help. Although the documentation for ContentResolver.registerContentObserver states:

Register an observer class that receives callbacks when the data identified by this content URI changes.

 Parameters uri The URI to watch for changes. This can be a specific row URI, or a base URI for a whole class of content. notifyForDescendents If true changes to URIs beginning with uri will also cause notifications to be sent. If false only changes to the exact URI specified by uri will cause notifications to be sent. If true, than any URI values at or below the specified URI will also trigger a match. 

(I set notifyForDescendents to false, but he shouldn't have called observers anyway).

What happened? thank you

+4
source share
2 answers

The content provider must decide when to report changes. For complex content providers (such as a contact provider), it is very difficult to identify all the specific URIs that change due to the operation, so they simply report a global change when something happens.

+1
source

The query parameters in your Uri, Fragment, or even Scheme are taken into account when matching Observer Uri. The only thing that matters is Uri Power and Segments of the path. Strict coincidence from left to right. I have not tested the "*" in the path segment to indicate a wildcard, but I suspect that it will not work.

Your particular observer is ContactsContract.RawContacts.CONTENT_URI, so anytime when any contact content changes for any reason, your observer will fire.

0
source

Source: https://habr.com/ru/post/1395194/


All Articles