setNotificationUri的机制是什么? [英] What's the mechanism of setNotificationUri?

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

问题描述

我刚刚实现了一个CursorLoader,它的工作太棒了!事实上,我不相信我的ListView会自动更新时,底层数据更改,直到我测试它。这显然是setNotificationUri的奇迹。



我的问题是,它是如何知道什么时候游标中的数据已经改变?说我静静地插入一个额外的行。底层机制不断地查询数据库并将其与过去的数据进行比较?如果数据集很大,那不是那么可怕的低效吗?



在我使用游标加载器之前,我需要手动刷新。这是很棒的,我不必这样做了,但它是有效的让CursorLoader在后台吗?

解决方案

<

$ c> query(...)方法:

  //告诉游标要观看什么uri,所以它知道它的源数据更改
cursor.setNotificationUri(getContext()。getContentResolver(),uri);

CursorLoader

  / *在工作线程上运行* / 
@Override
public Cursor loadInBackground(){
Cursor cursor = getContext()。getContentResolver()。query(mUri,mProjection,
mSelection,mSelectionArgs,mSortOrder);
if(cursor!= null){
//确保光标窗口已填充
cursor.getCount();
registerContentObserver(cursor,mObserver);
}
return cursor;
}

/ **
*注册观察器以在需要刷新游标时从内容提供程序
*获取通知。
* /
void registerContentObserver(Cursor cursor,ContentObserver observer){
cursor.registerContentObserver(mObserver);
}

当有人修改数据时, ContentProvider 通知 ContentResolver 有关更改:

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

ContentResolver 会通知所有注册的观察者



Observer,由 CursorLoader 注册,强制它加载新数据。


I've just implemented a CursorLoader and it works great! In fact, I didn't believe that my ListView would automatically update when the underlying data changed until I tested it. This apparently is the magic of setNotificationUri.

My question is, how does it know when the data in the cursor has changed? Say I quietly insert an additional row somewhere. Does the underlying mechanism constantly query the database and compare it with the past data? Won't that be horribly inefficient if the datasets are large?

Before I used cursorloaders, I would manually refresh when necessary. It's great that I don't have to do this anymore, but is it efficient to let the CursorLoader to this in the background?

解决方案

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆