【问题标题】:create method of DataSource.Factory doesn't get callDataSource.Factory 的创建方法没有被调用
【发布时间】:2019-05-05 15:20:41
【问题描述】:

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

问题是,当我通过 ViewHolder 的 init{} 方法中的 Paging 库调用 API 时,它运行良好。但在那之后,当我尝试编写其他内容并获取相关数据时,分页库甚至不调用 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)
        })
      )

    }

}

我的 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中写一些东西,所有需要的方法都会被调用,除了DataSource.Factorycreate()方法。

任何帮助将不胜感激。

【问题讨论】:

  • 您找到解决方案了吗?我正在经历同样的事情。
  • 我也有同样的问题......我的设置也和你的很相似

标签: android kotlin paging android-paging


【解决方案1】:

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

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

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

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() 之前 setObservers()

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

【讨论】:

  • 请分享一些例子
【解决方案2】:

尝试将 My Activity 中的 observe 方法移动到另一个方法,并在每次像这样调用 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)
        })
    }

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

【讨论】:

    猜你喜欢
    • 2012-03-13
    • 2014-06-02
    • 2011-08-28
    • 2017-02-24
    • 2015-02-23
    • 2013-12-19
    • 1970-01-01
    • 2020-09-19
    相关资源
    最近更新 更多