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

Android Paging Library LiveData<PagedList<T>> is triggered before the end of the api call

我尝试学习如何将新的分页库与 Kotlin 语言结合使用已经 2 天了(也是第一次)

所以我阅读了很多指南/教程和 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 会正确触发

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

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

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

loadInitial()loadAfter()loadBefore() 已经是异步的了。如果您尝试 println(Thread.currentThread().name),您将得到 arch_disk_io_0。要解决这个问题,您需要像同步一样执行代码,以免错过回调。如果你切换线程,你将错过回调,库已经在做。