我尝试通过两种方式迭代大型 Room 数据库:
1- 获取 Cursor 并对其进行迭代:
import android.database.Cursor
...
@Query("SELECT * FROM Category")
fun getAllCategory(): Cursor<Category>
...
val success = cursor.moveToFirst()
if (success) {
while (!cursor.isAfterLast) {
// Process items
cursor.moveToNext()
}
} else {
// Empty
}
cursor.close()
2- 使用PagedList 一次获取pageSize 数量的项目并进行处理。然后查询另一个页面并处理:
@Query("SELECT * FROM Category")
fun getAllCategory(): DataSource.Factory<Int, Category>
// Here i will return Flowable. You can return LiveData with 'LivePagedListBuilder'
fun getCategories(pageSize: Int): Flowable<PagedList<Category>> {
val config = PagedList.Config.Builder()
.setPageSize(pageSize)
.setPrefetchDistance(pageSize / 4)
.setEnablePlaceholders(true)
.setInitialLoadSizeHint(pageSize)
.build()
return RxPagedListBuilder(categoryDao.getAllCategory(), config)
.buildFlowable(BackpressureStrategy.BUFFER)
}
现在上面的getCategories() 函数将在Flowable 或LiveData 内返回pagedList。由于我们设置了setEnablePlaceholders(true),pagedList.size 将显示整个大小,即使它不在内存中。因此,如果pageSize 为 50,并且所有数据大小为 1000,pagedList.size 将返回 1000,但其中大部分为空。查询下一页并处理:
// Callback is triggered when next page is loaded
pagedList.addWeakCallback(pagedList.snapshot(), object : PagedList.Callback() {
override fun onChanged(position: Int, count: Int) {
for (index in position until (position + count)) {
if (index == (position + count - 1)) {
if (index < (pagedList.size - 1))
pagedList.loadAround(index + 1)
else{
// Last item is processed.
}
} else
processCurrentValue(index, pagedList[index]!!)
}
}
override fun onInserted(position: Int, count: Int) {
// You better not change database while iterating over it
}
override fun onRemoved(position: Int, count: Int) {
// You better not change database while iterating over it
}
})
// Start to iterate and query next page when item is null.
for (index in 0 until pagedList.size) {
if (pagedList[index] != null) {
processCurrentValue(index, pagedList[index]!!)
} else {
// Query next page
pagedList.loadAround(index)
break
}
}
结论:在PagedList 方法中,您可以一次获取数千行并进行处理,而在Cursor 方法中,您可以逐行迭代。当pageSize > 3000时我发现PagedList不稳定。它有时不返回页面。所以我使用了Cursor。在 Android 8 手机上使用这两种方法迭代(和处理)900k 行大约需要 5 分钟。