iro*_*oyo 6 android android-architecture-components android-paging
我想使用分页库,我正在使用协程从网络调用中获取数据。我已经验证从服务器正确检索了数据,但是当我从该方法调用回调时,我loadInitial在观察到的 LiveData 上一无所获。我已经尝试“模拟”服务器(以避免使用协程)并将结果传递给 Fragment。
当我使用协程时,我首先在大小为 0 的观察者上得到结果,然后当网络调用完成并使用回调时,我什么也没得到。问题似乎是我配置协程的方式。
用于协程的调度程序是否有问题?
这是 DataSource 的完整示例。使用的 CoroutineScope 是 ViewModel 提供的。
internal class CharactersDataSource(
private val coroutineScope: CoroutineScope,
private val characterRepository: CharacterRepository
) : PositionalDataSource<Character>() {
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<Character>) {
TODO()
}
override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<Character>) {
coroutineScope.launch {
val result = characterRepository.fetchCharacters(
params.requestedStartPosition,
params.requestedLoadSize
)
if (result is Output.Success) {
val value = result.value
// This next call should trigger data to be dispatched onto the LiveData
callback.onResult(value.results, 0, value.numberResultsTotal)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
332 次 |
| 最近记录: |