Difference between ContentObserver and DatasetObserver?
Asked Answered
I

4

31

What is difference between ContentObserver and DatasetObserver?

When one or another should be used?

I get Cursor with single row. I want to be notified about data changes - eg. when row is updated.

Which observer class should I register?

Insect answered 25/3, 2011 at 9:52 Comment(0)
A
30

If you are using a ContentProvider (via ContentResolver or Activity.managedQuery()) to get your data, simply attach a ContentObserver to your Cursor. The code in onChange() will be called whenever the ContentResolver broadcasts a notification for the Uri associated with your cursor.

Cursor myCursor = managedQuery(myUri, projection, where, whereArgs, sortBy);
myCursor.registerContentObserver(new ContentObserver() {
    @Override
    public void onChange(boolean selfChange) {
        // This cursor's Uri has been notified of a change
        // Call cursor.requery() or run managedQuery() again
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;
    }
}

Make sure your ContentProvider is a "good citizen" and registers the Uri with the cursor after a query:

cursor.setNotificationUri(getContentResolver(), uri);

It should also notify the ContentResolver of any changes to the underlying data (for instance, during insert, delete, and update operations on your SQLite database):

getContentResolver().notifyChange(uri, null);

This approach is a good example of the Observer Pattern of object-oriented design.

Amara answered 25/3, 2011 at 9:52 Comment(5)
Ok, and in which way DatasetObserver differs from ContentProvider?Insect
I haven't used a DataSetObserver, but it seems from the documentation (developer.android.com/reference/android/database/…) a DataSetObserver is notified of cursor lifecycle changes such as closing and requerying, whereas ContentObserver is used to watch for changes in underlying data. Hope that helps.Amara
Now I see. DatasetObserver observes Cursor state, whereas ContentProvider observes underlying content changes. Thanks!Insect
requery() is now deprecated, has the method for doing this changed?Antipus
Using managedQuery is also deprecated. The currently recommended approach is to asynchronously query your ContentProvider with CursorLoaders. They observe changes and automatically requery their cursors. See this: #7183420Amara
S
6

I'm not sure if this question is still on anyone's radar. I have been struggling with the same question for a little while now. What I came up with as my litmus test for deciding whether to use a DataSet Observer or a ContentObserver is pretty straight-forward:

If I need to send a URI in my notification I use a ContentObserver. If I simply need to notify one object that another object has changed -- I use a DataSetObserver.

The delimiting factor, for me at least, is does the object that is sending out the notification expose it's underlying resources (be they objects, records, queries, or cursors) as "Universal Resource Identifiers" to the rest of the application; or does the object hide the source of its data.

Solstice answered 27/7, 2016 at 12:34 Comment(0)
M
2

To provide the supplement to ptc's answer, DataSetObserver is used for handling content changes in the Adapter, for example, it can be used for updating listview dynamically with Adapter. You can register a DataSetObserver using the Adapter#registerDataSetObserver() method.

DataSetObserver can also be used to observe the content changes in the Cursor in a similar fashion.

Metaphrase answered 3/4, 2016 at 15:55 Comment(0)
C
1

From my last app developed I can say. The main difference between ContentObserver and DataSetObserver, is that ContentObserver makes to Observer any change affects on ContentProvider. On the other hand, DataSetObserver Observer any change effect on the database.

Chauffer answered 16/7, 2013 at 23:58 Comment(1)
DatasetObserver can be registered with a BaseAdapter - which isn't constrained solely to a database.Membranophone

© 2022 - 2024 — McMap. All rights reserved.