下面列出了怎么用android.database.AbstractWindowedCursor的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Performs the SQLiteQuery on the given database.
* <p>
* This util method encapsulates copying the cursor if the {@code maybeCopy} parameter is
* {@code true} and either the api level is below a certain threshold or the full result of the
* query does not fit in a single window.
*
* @param db The database to perform the query on.
* @param sqLiteQuery The query to perform.
* @param maybeCopy True if the result cursor should maybe be copied, false otherwise.
* @param signal The cancellation signal to be attached to the query.
* @return Result of the query.
*/
@NonNull
public static Cursor query(@NonNull RoomDatabase db, @NonNull SupportSQLiteQuery sqLiteQuery,
boolean maybeCopy, @Nullable CancellationSignal signal) {
final Cursor cursor = db.query(sqLiteQuery, signal);
if (maybeCopy && cursor instanceof AbstractWindowedCursor) {
AbstractWindowedCursor windowedCursor = (AbstractWindowedCursor) cursor;
int rowsInCursor = windowedCursor.getCount(); // Should fill the window.
int rowsInWindow;
if (windowedCursor.hasWindow()) {
rowsInWindow = windowedCursor.getWindow().getNumRows();
} else {
rowsInWindow = rowsInCursor;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || rowsInWindow < rowsInCursor) {
return CursorUtil.copyAndClose(windowedCursor);
}
}
return cursor;
}
private FastCursor(@NonNull AbstractWindowedCursor cursor) {
backingCursor = cursor;
count = cursor.getCount(); // fills cursor window
window = cursor.getWindow();
windowStart = window.getStartPosition();
windowEnd = windowStart + window.getNumRows();
position = -1;
}
public static void upgradeCursorWindowSize(final Cursor cursor) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
if (cursor instanceof AbstractWindowedCursor) {
final AbstractWindowedCursor windowedCursor = (AbstractWindowedCursor) cursor;
windowedCursor.setWindow(new CursorWindow("4M", 4 * 1024 * 1024));
}
if (cursor instanceof SQLiteCursor) {
((SQLiteCursor) cursor).setFillWindowForwardOnly(true);
}
}
}
public static void upgradeCursorWindowSize(final Cursor cursor) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
if (cursor instanceof AbstractWindowedCursor) {
final AbstractWindowedCursor windowedCursor = (AbstractWindowedCursor) cursor;
windowedCursor.setWindow(new CursorWindow("4M", 4 * 1024 * 1024));
}
if (cursor instanceof SQLiteCursor) {
((SQLiteCursor) cursor).setFillWindowForwardOnly(true);
}
}
}
static Cursor tryCreate(@NonNull Cursor cursor) {
if (cursor instanceof AbstractWindowedCursor) {
return new FastCursor((AbstractWindowedCursor) cursor);
}
return cursor;
}