What's the mechanism of setNotificationUri?

拜拜、爱过 提交于 2019-12-02 15:51:22

Please, correct me if I'm wrong somewhere.

ContentProvider calls something like this in query(…) method:

// Tell the cursor what uri to watch, so it knows when its source data changes
cursor.setNotificationUri(getContext().getContentResolver(), uri);

CursorLoader get cursor back and registers an observer.

/* Runs on a worker thread */
@Override
public Cursor loadInBackground() {
    Cursor cursor = getContext().getContentResolver().query(mUri, mProjection,
            mSelection, mSelectionArgs, mSortOrder);
    if (cursor != null) {
        // Ensure the cursor window is filled
        cursor.getCount();
        registerContentObserver(cursor, mObserver);
    }
    return cursor;
}

/**
 * Registers an observer to get notifications from the content provider
 * when the cursor needs to be refreshed.
 */
void registerContentObserver(Cursor cursor, ContentObserver observer) {
    cursor.registerContentObserver(mObserver);
}

When someone modifies data, ContentProvider notifies ContentResolver about changes:

getContext().getContentResolver().notifyChange(uri, null);

ContentResolver in its turn notifies all registered observers.

Observer, registered by CursorLoader, forces it to load new data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!