Android分页库LiveData< PagedList< T>>在api调用结束之前触发 [英] Android Paging Library LiveData<PagedList<T>> is triggered before the end of the api call

查看:582
本文介绍了Android分页库LiveData< PagedList< T>>在api调用结束之前触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经2天了,我试图学习如何将新的分页库与Kotlin语言一起使用

It's been 2 days I'm trying to learn how to use the new Paging Library with the Kotlin Language (first time too)

因此,我已经阅读了许多指南/教程和Github回购协议( https://github.com/STAR-ZERO/paging-retrofit-sample )来实现此分页库,基本上我的麻烦是我的ViewModel中的LiveData<PagedList<Discover>>是在我的api调用结束之前触发的,我没有知道为什么,我觉得呼叫callback.onResult(it?.results.orEmpty(), null, 2)没有任何作用

So I've read many guide / tutorial and Github repo (https://github.com/STAR-ZERO/paging-retrofit-sample) for implementing this paging library and basically my trouble is my LiveData<PagedList<Discover>> inside my ViewModel is trigger before my api call is ending and I don't know why and I feel the call callback.onResult(it?.results.orEmpty(), null, 2) doesn't do anything

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

I'm using this version android.arch.paging:runtime:1.0.1 You can find the repo of my project here : 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

Well, after many changed i've found something who fix the problem but it's so weird. If I use enqueue with Retrofit 2.3 it will doesn't work but if i do a .execute() the LiveData is correctly triggered

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

If someone have a better explanation of this problem your very welcome !

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

Edit : I've just read the Paging library overview page on android website and i found this :

要显示来自后端服务器的数据,请使用同步版本的 Retrofit API将信息加载到您自己的自定义DataSource中 对象.

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&lt; PagedList&lt; T&gt;&gt;在api调用结束之前触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆