- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以,我在玩 Android 的新 Paging Library JetPack 工具的一部分。
我正在做一个非常基本的演示应用程序,我在其中显示了我从 RandomUser 获得的随机用户配置文件列表。 API。
重要的是我已经设置好一切,而且它实际上可以正常工作。
在我的列表 fragment 中,我正在监听数据库更改:
...
mainViewModel.userProfiles.observe(this, Observer { flowableList ->
usersAdapter.submitList(flowableList) })
...
(我尝试同时使用 Flowables
和 Observables
以及 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 中的 Flowable
、Observable
或 LiveData
才会被触发,当UserProfileDataSource
从数据库返回一些结果。如果 UserProfileDataSource
没有返回任何结果,BoundaryCallback 被调用,对 API 的请求成功完成,数据被存储在数据库中,但是 Flowable
永远不会被触发。如果我关闭应用程序并再次打开它,将会获取我们刚刚从数据库中获取的数据,然后正确显示它。
我已经尝试了我在搜索的所有文章、教程和 github 项目中看到的所有可能的解决方案。
正如我所说,我试图将 RxPagedListBuilder
更改为 LivePagedListBuilder
。
当使用 RxPagedListBuilder
时,我尝试了 Flowables
和 Observables
。
我尝试使用不同的调度程序为 PagedList.Config.Builder
设置不同的配置。
我真的别无选择,我整天都在为此苦苦挣扎,任何提示都将不胜感激。
如果您需要有关代码的更多信息,请给我留言,我会更新帖子。
更新:
我已将完整的源代码提交到我的 Github repo
最佳答案
好的,我发现了错误:
我刚刚删除了 UserProfileDataSourceFactory
和 UserProfileDataSource
类。取而代之的是,我将此方法添加到我的 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/
我的应用程序中有大约 100 多个表。大多数更新查询不会更新最后更新的时间列。有 100 多个查询,所以我有点怀疑修改所有查询以包括上次更新时间。我正在寻找的替代选项是为所有表创建一个 oracle
我的应用程序中有大约 100 多个表。大多数更新查询不会更新最后更新的时间列。有 100 多个查询,所以我有点怀疑修改所有查询以包括上次更新时间。我正在寻找的替代选项是为所有表创建一个 oracle
trigger.io 是否支持黑莓?如果没有,是否有任何计划支持它。 最佳答案 在移动方面,他们目前支持: iOS 4+ (iPhone 和 iPad) 安卓 2.0+ window 电话* 我不知道
这是 here 中的一个改写的问题。经过一些测试后,我隔离了问题,但没有解决它的线索。无需阅读上一个问题,这是简化的代码: 问题 -> trigger('click')执行,但在循环(间隔)函数内时不
我有一个程序,它会从 redis 数据存储中轮询某个键,并在该值满足某个条件时执行某些操作。 不过我觉得redis周期性的轮询效率很低,想知道redis有没有“触发”机制,当值发生变化满足条件时,触发
现在我的合作者收到这样的错误:[错误] Forge API call to app/f7617c848[...]adcbe/should_rebuild 出错:应用匹配查询不存在。 最佳答案 我们正在
当用户在我的 trigger.io iOS 应用程序(使用 Parse.com)上收到推送通知并希望在用户打开应用程序时清除徽章时,我将徽章设置为 1。有谁知道如何做到这一点? 谢谢! 最佳答案 有几
iPhone 启动图像现在显示太短,我想延长持续时间。 有没有办法在 config.json 中指定它? 最佳答案 更新 :从平台版本 v1.4.20 开始,您可以控制启动图像的可见性(http://
如何将文件下载到我的 trigger.io 应用程序?我的应用程序显示离线数据,但我需要我的应用程序如何从服务器下载和存储本地新鲜数据的方式。数据是一个带有项目数组和一些图像的 js 文件,每个图像都
为了根据条件更改 ListBoxItem 前景,我可以通过 DataTemplate.Triggers 来完成: ...
为了根据条件更改 ListBoxItem 前景,我可以通过 DataTemplate.Triggers 来完成: ...
我们正在构建一个以 Chaplin 为基础的 Trigger 应用程序。出于开发目的,如果我们可以使用 Assets 的绝对路径,那就太好了,比如: 是否可以在 Trigger 中执行此操作?
什么时候应该选择Style.Triggers,什么时候应该选择ControlTemplate.Triggers?使用一个比另一个有什么好处吗? 假设我有实现相同结果的这些样式:
在我的情况下,我将有一个带有自定义 url 架构的重定向 URL,例如 myapp://oauth 所以首先我会转发到我的 oauth 登录 url,然后我需要为位置更改注册一个事件处理程序,以检查它
我可以创建一个带有 OR 条件的 MYSQL TRIGGER 创建或替换触发器 TIGGER 名称插入之前或更新或删除 像这样(下面是 ORACLE TRIGGER): CREATE OR REPLA
有两个相同类型但存在于不同类中的属性。 FrameworkElement.Triggers Property Style.Triggers Property 这让我很困惑,所以我很少有问题要寻找答案:
我有兴趣为 trigger.io 编写 native 插件。 这是否已经可能或在路线图上? 最佳答案 这绝对在路线图上:我们从一开始就不允许这样做的主要原因是我们最初的主要重点是创建一个尽可能平稳和快
我知道如何缓存它,但我以后如何检索它。文档 ( http://docs.trigger.io/en/v1.4/features/cache.html ) 仅给出了使用图像的示例,要检索您需要做的就是获
在 AzureFunction1 中收到简短通知后,我需要一种可靠的方法将其快速保存,然后触发另一个 Azurefunction 来分析数据并根据分析进行其他操作。 我正在寻找一种向 azure fu
我正在尝试使用 Trigger.io 的框架构建一个流式音频应用程序。 看到它的 API 目前没有任何与音频相关的方法,我用基本的 HTML5 audio 对其进行了测试。标签。在这种情况下流式传输
我是一名优秀的程序员,十分优秀!