gpt4 book ai didi

android - 分页库 : Saving data in the DB doesn't trigger the UI changes

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:49 25 4
gpt4 key购买 nike

所以,我在玩 Android 的新 Paging Library JetPack 工具的一部分。

我正在做一个非常基本的演示应用程序,我在其中显示了我从 RandomUser 获得的随机用户配置文件列表。 API。

重要的是我已经设置好一切,而且它实际上可以正常工作。

在我的列表 fragment 中,我正在监听数据库更改:

...
mainViewModel.userProfiles.observe(this, Observer { flowableList ->
usersAdapter.submitList(flowableList) })
...

(我尝试同时使用 FlowablesObservables 以及 RxPagedListBuilder 现在我正在使用 LiveData 使用 LivePagedListBuilder。如果可能的话,我想使用 RxPagedListBuilder)

在我的 ViewModel 中,我正在设置 LivePagedListBuilder

...
private val config = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(UserProfileDataSource.PAGE_SIZE / 2) //Is very important that the page size is the same everywhere!
.setPageSize(UserProfileDataSource.PAGE_SIZE)
.build()

val userProfiles: LiveData<PagedList<UserProfile>> = LivePagedListBuilder(
UserProfileDataSourceFactory(userProfileDao), config)
.setBoundaryCallback(UserProfileBoundaryCallback(randomUserRepository, userProfileDao))
.build()
...

我已将数据源设置为从我的数据库中获取数据(如果有的话):

class UserProfileDataSource(val userDao: UserDao): PageKeyedDataSource<Int, UserProfile>() {

companion object {
const val PAGE_SIZE = 10
}

override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, UserProfile>) {
callback.onResult(userDao.getUserProfileWithLimitAndOffset(PAGE_SIZE, 0), 0, PAGE_SIZE)
}

override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, UserProfile>) {
callback.onResult(userDao.getUserProfileWithLimitAndOffset(PAGE_SIZE, params.key), params.key + PAGE_SIZE)
}

override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, UserProfile>) {
//This one is not used
}
}

如果数据库为空,我已经将 BoundaryCallback 设置为从网络获取数据:

class UserProfileBoundaryCallback(val userRepository: RandomUserRepository,
val userDao: UserDao) : PagedList.BoundaryCallback<UserProfile>() {


/**
* Database returned 0 items. We should query the backend for more items.
*/
override fun onZeroItemsLoaded() {
userRepository.getUserProfiles(0, UserProfileDataSource.PAGE_SIZE)
.subscribeOn(Schedulers.io()) //We shouldn't write in the db over the UI thread!
.subscribe(object: DataSourceSubscriber<List<UserProfile>>() {
override fun onResultNext(userProfiles: List<UserProfile>) {
userDao.storeUserProfiles(userProfiles)
}
})
}

/**
* User reached to the end of the list.
*/
override fun onItemAtEndLoaded(itemAtEnd: UserProfile) {
userRepository.getUserProfiles(itemAtEnd.indexPageNumber + 1, UserProfileDataSource.PAGE_SIZE)
.subscribeOn(Schedulers.io()) //We shouldn't write in the db over the UI thread!
.subscribe(object: DataSourceSubscriber<List<UserProfile>>() {
override fun onResultNext(userProfiles: List<UserProfile>) {
userDao.storeUserProfiles(userProfiles)
}
})
}

}

只有当数据来自数据库时,UI 中的 FlowableObservableLiveData 才会被触发,当UserProfileDataSource 从数据库返回一些结果。如果 UserProfileDataSource 没有返回任何结果,BoundaryCallback 被调用,对 API 的请求成功完成,数据被存储在数据库中,但是 Flowable 永远不会被触发。如果我关闭应用程序并再次打开它,将会获取我们刚刚从数据库中获取的数据,然后正确显示它。

我已经尝试了我在搜索的所有文章、教程和 github 项目中看到的所有可能的解决方案。

正如我所说,我试图将 RxPagedListBuilder 更改为 LivePagedListBuilder

当使用 RxPagedListBuilder 时,我尝试了 FlowablesObservables

我尝试使用不同的调度程序为 PagedList.Config.Builder 设置不同的配置。

我真的别无选择,我整天都在为此苦苦挣扎,任何提示都将不胜感激。

如果您需要有关代码的更多信息,请给我留言,我会更新帖子。

更新:

我已将完整的源代码提交到我的 Github repo

最佳答案

好的,我发现了错误:

我刚刚删除了 UserProfileDataSourceFactoryUserProfileDataSource 类。取而代之的是,我将此方法添加到我的 DAO 中:

@Query("SELECT * FROM user_profile")
fun getUserProfiles(): DataSource.Factory<Int, UserProfile>

我正在构建我的 RxPagedListBuilder,例如:

RxPagedListBuilder(userDao.getUserProfiles(), UserProfileDataSource.PAGE_SIZE)
.setBoundaryCallback(UserProfileBoundaryCallback(randomUserApi, userDao))
.buildObservable()

就是这样。我不确定为什么它不能与 DataSource 和 DataSourceFactory 一起使用,如果有人能解释这种奇怪的行为,我会将该答案标记为已接受,因为我的解决方案更像是一个幸运的机会。

不过现在发生的一件事是,当我将数据加载到适配器中时,适配器会滚动回顶部位置。

关于android - 分页库 : Saving data in the DB doesn't trigger the UI changes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50591745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com