装载机和onLoaderReset的Andr​​oid [英] Loaders and onLoaderReset Android

查看:454
本文介绍了装载机和onLoaderReset的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序实现的装载机从数据库中查询数据。我听这种情况发生通过实施修改 LoaderCallbacks<光标> 监听器。当我的数据变化,我想无效,并释放与加载器关联的任何数据的方法,我的问题是,使用 onLoaderReset时(装载程序装载<光标&GT)。在所有的例子,在该方法有下列调用:

I implemented a Loader in my application for querying data from the database. I listen the changes that happen' by implementing LoaderCallbacks<Cursor> listener. The problem that I have is when using the onLoaderReset(Loader<Cursor> loader) method when my data change and I want to invalidate and free any data associated with the loader. In all the examples, in this method there is the following call:

mAdapter.swapCursor(空);

但问题是我不从光标适配器使用的数据,我在我的应用程序的一些其他的方式来使用它。

But the thing is I don't use the data from the cursor in adapter, I use it in some other way in my application.

(直接从 onLoadFinished返回光标(装载机&LT;光标&GT;装载机,光标数据),例如)

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

            if (data.moveToFirst()) {
                TOTAL_CARDS = data.getCount();
                mView.createCards(TOTAL_CARDS);
            } else {
                TOTAL_CARDS = 0;
                mView.createCards(TOTAL_CARDS);
            }


        }

什么是相应的事情在这里,这是 mAdapter.swapCursor 相似。 我没有用装载机太多的经验,其实我刚开始和他们一起工作,所以如果有人有一个解决的办法,我会AP preciate它。 THX!

What would be the corresponding thing to do here, that is similar with mAdapter.swapCursor. I don't have much experience with loaders, in fact I just started working with them, so if someone has a solution to this, I would appreciate it. Thx!

编辑: 现在,我路过空,加载程序和它的工作原理,是这样的:

For now, I am passing null to the loader and it works, like this:

@Override
public void onLoaderReset(Loader<Cursor> loader) {
        loader = null;
}

};

但是,这是正确的解决方案?

But is this the right solution?

推荐答案

@Override
public void onLoaderReset(Loader<Cursor> loader) {
  loader = null;
}

是一样好,什么都不做。在您的例子code,你仅仅是的归零的你的方法的本地引用它的参数。但是,该文献将始终方法调用的返回后取出。 (您可能需要阅读就是Java&QUOT;传递按引用&QUOT;或&QUOT;传址 - 值&QUOT;?为主题的进一步讨论。)

is just as good as doing nothing. In your example code, you are merely nulling your method's local reference to its argument. However, this reference will always be removed after the return of the method call. (You might want to read Is Java "pass-by-reference" or "pass-by-value"? for a further discussion of the topic.)

onLoaderReset(装载机)方法被调用时,您的装载机的回调(通常是活动片段实例)被要求发布到光标,它通过 onLoadFinished(装载机,光标已经获得了所有引用)之前。基本上,这个方法请你来清理,因为装载机即将关闭光标之前提供给您。当光标被关闭了,你将不再能够通过它来获取数据。如果将光标但仍然在使用(通常由的CursorAdapter 你所说的)被关闭后,这会导致一个异常被抛出。

The onLoaderReset(Loader) method gets called when your loader's callback (usually an Activity or Fragment instance) is asked to release all references to the Cursor which it has gained via onLoadFinished(Loader, Cursor) before. Basically this method asks you to clean up since the Loader will soon close the Cursor it provided to you before. After the cursor was closed, you will not longer be able to retrieve data by it. If the cursor would however still be in use (typically by a CursorAdapter as you mentioned) after it was closed, this would cause an exception to be thrown.

同样, onLoadFinished(装载机,光标)有一个隐含的合同,要求该方法返回之后的任何原已光标对象不能再被使用。相反,你必须更换由设置作为方法的参数新的游标这些引用。与此相反, onLoaderReset(装载机)要求你履行相同的合同,但没有提供更换的,即你应该删除所有对一个以前检索光标

Similarly, onLoadFinished(Loader, Cursor) has an implicit contract asking that after the method returns any formerly provided Cursor objects must not longer be in use. Instead, you have to replace these references by the new cursor which is provided as a method argument. In contrast, onLoaderReset(Loader) asks you to fulfill the same contract, but without providing a replacement, i.e. you should remove all references to a formerly retrieved Cursor.

在你的榜样,你不要让你的光标逃脱方法范围,而是你正在阅读的数据的时候了。因此,没有必要删除任何引用到光标对象,它是通过 onLoadFinished(装载机,光标)提供因为有没有。一个空的实施 onLoaderReset(装载机)履行接口的合同,因此将做的工作给你。

In your example, you do not let your Cursor escape the method scope but instead you are reading the data right away. Therefore, it is not necessary to remove any references to a Cursor object which was provided via onLoadFinished(Loader, Cursor) since there are none. An empty implementation of onLoaderReset(Loader) to fulfill the interface contract will therefore do the job for you.

这篇关于装载机和onLoaderReset的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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