gpt4 book ai didi

Android分页库LiveData>在api调用结束前触发

转载 作者:行者123 更新时间:2023-12-03 22:08:07 29 4
gpt4 key购买 nike

已经两天了,我正在尝试学习如何将新的分页库与 Kotlin 语言一起使用(也是第一次)

所以我已经阅读了很多指南/教程和 Github repo (https://github.com/STAR-ZERO/paging-retrofit-sample) 来实现这个分页库,基本上我的问题是我的 LiveData<PagedList<Discover>>在我的ViewModel在我的 api 调用结束之前触发,我不知道为什么,我感觉到调用 callback.onResult(it?.results.orEmpty(), null, 2)什么都不做

我正在使用这个版本android.arch.paging:runtime:1.0.1你可以在这里找到我的项目的仓库:https://github.com/florian-do/TMDB
logcat :

D/DataSourceFactory: : create()
D/SequentialDataSource: loadInitial:
D/Interceptor: https://api.themoviedb.org/3/discover/movie?api_key=??
D/MainFragment: : observe 0
D/SequentialDataSource: response code -> 200
D/SequentialDataSource: list size: 20

这是我的代码:
Fragment.kt
    val adapter = DiscoverAdapter(context!!, diffCallBack)
binding.rvFeed.layoutManager = GridLayoutManager(context, 3)
binding.rvFeed.setHasFixedSize(true)
binding.rvFeed.adapter = adapter

viewModel.data.observe(this, Observer {
Log.d(TAG, ": observe "+it?.size)
})
MainViewModel.kt
class MainViewModel : ViewModel() {

var amount = ObservableField<String>()
val data : LiveData<PagedList<Discover>>

init {
val config = PagedList.Config.Builder()
.setPageSize(20)
.setEnablePlaceholders(false)
.build()

val api : DiscoverService = App.retrofit.create(DiscoverService::class.java)
val dataSourceFactory = DataSourceFactory(api)

data = LivePagedListBuilder(dataSourceFactory, config).build()
}
}
DataSourceFactory.kt
class DataSourceFactory(api: DiscoverService) : DataSource.Factory<Int, Discover>() {
val source = SequentialDataSource(api)

override fun create(): DataSource<Int, Discover> {
return source
}
}
SequentialDataSource.kt
class SequentialDataSource(val api : DiscoverService) : PageKeyedDataSource<Int, Discover>() {

private val TAG = "SequentialDataSource"

override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, Discover>) {
Log.d(TAG, "loadInitial: ")
api.getDiscover(TMDBClient.API_KEY).enqueue(object : Callback<DiscoverReponse> {
override fun onFailure(call: Call<DiscoverReponse>, t: Throwable) {
Log.d(TAG, ": FAIL")
}

override fun onResponse(call: Call<DiscoverReponse>, response: Response<DiscoverReponse>) {
Log.d(TAG, ": response code -> "+response.code())
val it = response.body();
Log.d(TAG, "list size: "+it?.results?.size)

response.body()?.let {
callback.onResult(it.results, null, 2)
}
}

})

}

override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Discover>) {
Log.d(TAG, "loadAfter: "+params.key)

}

override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, Discover>) {
Log.d(TAG, "loadBefore: "+params.key)
}
}

最佳答案

好吧,经过多次更改后,我找到了解决问题的方法,但这太奇怪了。
如果我使用 enqueue使用 Retrofit 2.3 它将不起作用,但如果我执行 .execute() LiveData 被正确触发

如果有人对这个问题有更好的解释,非常欢迎!

编辑 :
我刚刚阅读了Paging library overview android网站上的页面,我发现了这个:

To display data from a backend server, use the synchronous version of the Retrofit API to load information into your own custom DataSource object.

关于Android分页库LiveData<PagedList<T>>在api调用结束前触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53016777/

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