Cursor#getCount 用来保证内容window被填满?

Cursor#getCount is used to ensure content window is filled?

下面是我正在阅读的 Android 教程书中的代码片段。 loadInBackground 得到一个游标然后做 cursor.getCount() 到 "ensure that the content window is filled"。这是什么意思? getCount 上的文档只是说 "returns number of rows in the cursor"。我用 Google 搜索了这个 "ensure that the content window is filled" 并且有相当多的片段执行此操作,所有片段都有相同的评论,但没有解释为什么这是 needed/how 它有效。

import android.content.AsyncTaskLoader;
import android.content.Context;
import android.database.Cursor;

public abstract class SQLiteCursorLoader extends AsyncTaskLoader<Cursor> {

    private Cursor cursor;

    public SQLiteCursorLoader(Context context) {
        super(context);
    }

    protected abstract Cursor loadCursor();

    @Override
    public Cursor loadInBackground() {
        Cursor cursor = loadCursor();
        if (cursor != null) {
            cursor.getCount(); // ensure that the content window is filled
        }
        return cursor;
    }

}

如您所知,数据库 return 在 query 之后是 Cursor。但是,只有当您尝试读取某些信息时,光标才会有效地填充数据,例如:cursor.getCount()cursor.moveToFirst() 等...

这在大型查询期间更为明显。

例如,假设下面的查询会 return 千个结果:

Cursor cursor = db.rawQuery("select * from TABLE", null);

然而,该声明不会花费太多时间 运行...

但是,当您第一次最终调用 cursor.getCount()cursor.moveToFirst() 时,您可能会看到一些 "lag",因为游标正在有效地填充数据库中的数据.

如果您在 UI 主线程中执行此操作,您的应用可能会冻结几秒钟。特别是在低端设备上。

因此,通过调用该方法,我相信作者试图确保数据在 loadInBackground() 期间完全加载。这样,他确保数据在后台加载,而不是在任何其他未来方法中加载。这样,以后对 getCount()moveToFirst() 的任何调用都将非常快速地执行,因为数据已经加载。

反正不是强制的..