我正在考虑在我的应用程序中使用 android Room 库作为 ORM,但我想知道更多详细信息/评论,因为我有一些限制并且我无法在互联网上找到(即 Google;))我执行以下查询:
@Query("SELECT * FROM users")
List<User> getAll();
如果我有成千上万的用户,这不是问题吗?因为从下面生成的代码中,它似乎将所有内容都加载到了 ArrayList 中。事件 LiveData> 或 Flowable> 做同样的事情。
@Override
public List<User> getAll() {
final String _sql = "SELECT * FROM Users";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
final Cursor _cursor = __db.query(_statement);
try {
final int _cursorId = _cursor.getColumnIndexOrThrow("id");
final int _cursorName = _cursor.getColumnIndexOrThrow("name");
final List<User> _result = new ArrayList<User>(_cursor.getCount());
while(_cursor.moveToNext()) {
final User _item;
final String _tmpMId;
_tmpMId = _cursor.getString(_cursorId);
final String _tmpMName;
_tmpMName = _cursor.getString(_cursorName);
_item = new User(_tmpMId,_tmpMName);
_result.add(_item);
}
return _result;
} finally {
_cursor.close();
_statement.release();
}
}
@Override
public Flowable<List<User>> getAllRX() {
final String _sql = "SELECT * FROM Users";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
return RxRoom.createFlowable(__db, new String[]{"Users"}, new Callable<List<CachedAttendee>>() {
public List<User> call() throws Exception {
final Cursor _cursor = __db.query(_statement);
try {
final int _cursorId = _cursor.getColumnIndexOrThrow("id");
final int _cursorName = _cursor.getColumnIndexOrThrow("name");
final List<User> _result = new ArrayList<User>(_cursor.getCount());
while(_cursor.moveToNext()) {
final User _item;
final String _tmpMId;
_tmpMId = _cursor.getString(_cursorId);
final String _tmpMName;
_tmpMName = _cursor.getBlob(_cursorName);
_item = new User(_tmpMId,_tmpMName);
_result.add(_item);
}
return _result;
} finally {
_cursor.close();
}
}
@Override
protected void finalize() {
_statement.release();
}
});
}
我是看错了还是谷歌忽略了这一点?我总是可以使用游标,但这违背了让 ORM 为我处理序列化的意义。
干杯,