我从两个来源学习功能分页库。我找到了使用边界回调的简单方法,但我很困惑。
哪个方法将在主线程中首先被调用: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) }
)
}