首页 > 解决方案 > DataSource.Factory 的创建方法没有被调用

问题描述

我试图用分页库制作一个字典应用程序。当用户写任何字母时,我会调用一个 API 并获取包括该字母在内的所有单词。

问题是,当我在init{}ViewHolder 的方法中通过 Paging 库调用 API 时,它运行良好。但在那之后,当我尝试编写其他内容并获取相关数据时,分页库甚至不调用 API。我在每个地方都放了断点,我看到第一次之后,create()DataSource.Factory 的方法没有被调用。

这是我的代码:

我的活动:

    viewModel = ViewModelProviders.of(this).get(DictionaryViewModel::class.java)
    val adapter = DictionaryRecyclerPagedAdapter()
    recycler.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
    recycler.setHasFixedSize(true)
    recycler.adapter = adapter

    RxTextView.textChanges(edtWord)
        .filter { it.isNotBlank() }
        .subscribe({
            adapter.submitList(null)
            viewModel.getWord(it.toString())
            Logger.d(it)

        }, {
            Logger.d(it)
        })



    viewModel.itemPagedList?.observe(this, Observer {
        adapter.submitList(it)
    })

我的视图模型:

var liveDataSource: LiveData<PageKeyedDataSource<Int, DictionaryResult>>? = null
var itemPagedList: LiveData<PagedList<DictionaryResult>>? = null

init {

    getWord("pouya")
}

fun getWord(term: String) {

    val dataSourceFactory = DictionaryDataFactory(term)
    liveDataSource = dataSourceFactory.liveData


    val config = PagedList.Config.Builder()
        .setEnablePlaceholders(true)
        .setPageSize(10)
        .setPrefetchDistance(4)
        .build()

    itemPagedList = LivePagedListBuilder(dataSourceFactory, config).build()

}

我的数据源:

   class DictionaryDataSource(private val term: String) : PageKeyedDataSource<Int, DictionaryResult>() {

override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, DictionaryResult>) {
    Logger.d("Initial")
    composite.add(
        repository.getWord(FIRST_PAGE, term)
            .subscribe({
                if (it.result != null)
                    callback.onResult(it.result, null, FIRST_PAGE + 1)
            }, {
                Logger.d(it)

            })
    )
}

override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, DictionaryResult>) {
    Logger.d("AFTER")
    composite.add(repository.getWord(params.key, term)
        .subscribe({
            if (it.result != null)
                callback.onResult(it.result, params.key + 1)
        }, {
            Logger.d(it)
        })
      )

    }

}

我的 DataSource.Factory :

  class DictionaryDataFactory(private val term: String) : DataSource.Factory<Int, DictionaryResult>() {

val liveData = MutableLiveData<PageKeyedDataSource<Int, DictionaryResult>>()

override fun create(): DataSource<Int, DictionaryResult> {
    val dataSource = DictionaryDataSource(term)
    liveData.postValue(dataSource)
    return dataSource
    }
 }

所以请记住这一点,一旦我启动应用程序,它就会完美运行(它会获得"pouya"我在 ViewHolder 的方法中调用它的单词的所有含义init{})但是之后,当我在 edittext 中写一些东西时,所有需要的create()方法被调用,除了DataSource.Factory.

任何帮助将不胜感激。

标签: androidkotlinpagingandroid-paging

解决方案


我遇到了同样的问题,为我解决的问题是在设置观察者之前建立列表,在你的情况下,确保在观察 itemPagedList 之前调用 getWord()

例如:- 这是使用 viewModel.reponseList 的片段的片段

override fun onViewReady() {
    setRecyclerView()
    viewModel.buildResponseList() //For some reason responseList must be built before observing on it ,
    // otherwise create() in FoodDataSourceFactory won't be called
    setObservers()
}

private fun setObservers() {
    viewModel.responseList.observe(this, Observer {
        (this.rvFoodList.adapter as FoodListAdapter).submitList(it)
    })
}

private fun setRecyclerView() {
    val mRecyclerView = this.rvFoodList
    mRecyclerView.apply {
        layoutManager = LinearLayoutManager(this@HomeFragment.context)
        adapter = FoodListAdapter()
    }

视图模型部分:-

fun buildResponseList() {
    val foodDataSourceFactory =
        FoodDataSourceFactory(this)

    val config = PagedList.Config.Builder()
        .setInitialLoadSizeHint(30)
        .setEnablePlaceholders(false)
        .setPageSize(30)
        .build()

    responseList = LivePagedListBuilder(foodDataSourceFactory, config).build()
}

在 onViewReady()

如果我在调用 viewModel.buildResponseList() 之前 setObservers()

DataSourceFactory 类不会调用 create() 方法,因此不会在 DataSource 类中进行网络调用


推荐阅读