@Entity(tableName = "items")
class Item {
@PrimaryKey
var id=0
@ColumnInfo(name = "name")
var name=""
@ColumnInfo(name = "thumbnail")
var thumbnail=""
}
@RawQuery(observedEntities = [Item::class])
fun select(query:SupportSQLiteQuery):Flowable<List<Item>>
@RawQuery(observedEntities = [Item::class])
fun selectCount(query: SupportSQLiteQuery):Int
//query example
//select id,name,thumbnail from items
//where (name like '%ruby%' and name like '%diamond%') and
//(thumbnail like '%lion%' and thumbnail like '%tiger%')
DB.getInstance().Dao().select(query)
.concatMapIterable { it }
.concatMap {
//blabla...
}
selectCount的周转时间非常短,但是使用select时,它每秒发出近一个项目。 (当两个函数的条件完全相同时)
即使我直接使用SQLiteOpenHelper访问相同的查询,它的测量速度也比使用Room快10倍。
在提高速度方面我有什么想念的吗?创建数据库实例时,使用以下功能。
fun getInstance(): DB {
if (INSTANCE == null) {
synchronized(DB::class) {
INSTANCE = Room.databaseBuilder(
getContext(),
DB::class.java,
"test.db")
.build()
}
}
return INSTANCE!!
}