本文介绍了什么是cursor.setNotificationUri()用于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经研究过如何使用 ContentProviders 和载入器从



我如何看待它:
我们有一个 code>与 ListView SimpleCursorAdapter CursorLoader 。我们还实现 ContentProvider



活动可以通过按钮单击来调用 getContentResolver()。insert(URI,contentValues);



ContentProvider 在结束insert()方法中我们调用 getContentResolver()。notifyChange(URI,null); code> CursorLoader 将接收到它应该重新加载数据和更新UI的消息。如果我们在 SimpleCursorAdapter 中使用 FLAG_REGISTER_CONTENT_OBSERVER ,它也将接收消息及其方法 onContentChanged 将被调用。



因此,如果我们插入,更新或删除数据,我们的ListView将会更新。



Activity.startManagingCursor(cursor); 已弃用, cursor.requery()已弃用,所以我没有看到任何从 cursor.setNotificationUri()



我查看了 setNotificationUri()方法的源代码,并看到它在方法中调用 mContentResolver.registerContentObserver(mNotifyUri,true,mSelfObserver)。另外 CursorLoader 也是这样。最后光标将接收消息,并在Cursor中调用以下方法:

  protected void onChange(boolean selfChange){
synchronized(mSelfObserverLock){
mContentObservable.dispatchChange(selfChange,null);
// ...
}
}


所以我的问题是:为什么要调用 cursor.setNotificationUri() ContentProvider 实施的查询()方法?


如果你调用 Cursor.setNotificationUri(),Cursor会知道什么是ContentProvider Uri

em> it is created for。



CursorLoader 注册自己的 ForceLoadContentObserver code> ContentObserver )与 Context ContentResolver ContentResolver 时指定 setNotificationUri


$ b [这发生在你调用 getContext()。getContentResolver()。notifyChange(uri,contentObserver); code> ContentProvider 的 insert() update() code> delete() methods]它会通知所有观察者,包括CursorLoader的 ForceLoadContentObserver



ForceLoadContentObserver ,然后将Loader的mContentChanged标记为true


I did research on how to use ContentProviders and Loaders from this tutorial

How I see it:We have an Activity with ListView, SimpleCursorAdapter and CursorLoader. We also implement ContentProvider.

In an Activity we can call getContentResolver().insert(URI, contentValues); via a button click.

In our implementation of ContentProvider in the end insert() method we call getContentResolver().notifyChange(URI, null); and our CursorLoader will receive message that it should reload data and update UI. Also if we use FLAG_REGISTER_CONTENT_OBSERVER in SimpleCursorAdapter it will also receive message and its method onContentChanged() will be called.

So our ListView will be updated if we insert, update or delete data.

Activity.startManagingCursor(cursor); is deprecated, cursor.requery() deprecated, so I do not see any practice sense from cursor.setNotificationUri().

I looked into setNotificationUri() method's source code and saw that it calls mContentResolver.registerContentObserver(mNotifyUri, true, mSelfObserver) inside the method. Also CursorLoader does the same. Finally cursor will receive message and the following method will be called inside Cursor:

protected void onChange(boolean selfChange) {
    synchronized (mSelfObserverLock) {
        mContentObservable.dispatchChange(selfChange, null);
        // ...
    }
}

But I can not make sense of this.

So my question is: why should we call cursor.setNotificationUri() in query() method of our ContentProvider implementation?

解决方案

If you call Cursor.setNotificationUri(), Cursor will know what ContentProvider Uri it was created for.

CursorLoader registers its own ForceLoadContentObserver (which extends ContentObserver) with the Context's ContentResolver for the URI you specified when calling setNotificationUri.

So once that ContentResolver knows that URI's content has been changed [ this happens when you call getContext().getContentResolver().notifyChange(uri, contentObserver); inside ContentProvider's insert(), update() and delete() methods ] it notifies all the observers including CursorLoader's ForceLoadContentObserver.

ForceLoadContentObserver then marks Loader's mContentChanged as true

这篇关于什么是cursor.setNotificationUri()用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:11