DataSource.Factory 的创建方法没有被调用

问题描述 投票:0回答:2

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

问题是,当我在 ViewHolder 的

init{}
方法中通过 Paging 库调用 API 时,它工作得很好。但之后,当我尝试编写其他内容并获取相关数据时,Paging 库甚至不调用 API。我在每个地方都放置了断点,我发现第一次之后,DataSource.Factory 的
create()
方法没有被调用。

这是我的代码:

我的活动:

    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)
        })
      )

    }

}

我的数据源.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{}
方法中所说的那样),但之后,当我写一些东西时在编辑文本中,除了
create()
DataSource.Factory
方法之外,所有需要的方法都被调用。

任何帮助将不胜感激。

android kotlin android-paging android-paging-library
2个回答
7
投票

我也遇到了同样的问题,为我解决的是在设置观察者之前构建列表,在您的情况下,请确保在观察 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([email protected])
        adapter = FoodListAdapter()
    }

ViewModel 部分:-

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() 之前设置Observers()

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


0
投票

尝试将“我的活动”中的观察方法移至另一个方法,并在每次调用 viewModel.getWord() 时调用该方法,如下所示

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

        }, {
            Logger.d(it)
        })



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

这个问题可能有更好的解决方案,但这对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.