我从两个来源研究功能分页库。我找到了使用boundaryCallback进行操作的简单方法,但是我很困惑。
哪个方法将首先在主线程上被调用:boundaryCallback方法或Rx观察器方法?
因为我们有两个不同的工作线程,一个线程是Rx线程,它从网络加载数据,另一个线程是Room线程,当rx线程完成插入事务时,该线程从数据库加载数据。
// My ViewModel
init {
App.instance.appComponent.inject(this)
employeeList = repositoryEmployee.getAll().toLiveData(pageSize = 10,
boundaryCallback = object : PagedList.BoundaryCallback<Employee>(){
override fun onZeroItemsLoaded() {
super.onZeroItemsLoaded()
view?.setProgressVisible(visible = true)
loadFromServer()
}
override fun onItemAtEndLoaded(itemAtEnd: Employee) {
super.onItemAtEndLoaded(itemAtEnd)
loadFromServer()
}
})
}
private fun loadFromServer(){
if (isLoading) return
isLoading = true
disposable = hostedRestApi.getEmployees(currentPage)
.doOnSuccess { response -> repositoryEmployee.insertList(response.data.map {
Employee(it.id, it.firstName, it.lastName, it.email) })
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ currentPage++
isLoading = false
view?.setProgressVisible(visible = false)},
{throwable ->
throwable.message?.let { message -> view?.showToast(message) }
view?.setProgressVisible(visible = false) }
)
}