【问题标题】:dataSource.inValidate() not working Paging librarydataSource.inValidate() 不工作分页库
【发布时间】:2019-01-07 10:33:29
【问题描述】:

我正在使用具有 MVP 架构的分页库。数据源是在交互器中创建的,我正在尝试使 Fragment 中的数据源无效,如下所示。

我的片段

class MyFragment : BaseFragment<MyFragment>(), MyView {

@Inject
lateinit var mPresenter: ActivePrescriptionPresenter<ActivePrescriptionView>
private var isRefreshRequest = false // Used to handle the visibility of toast message and Swipe to Refresh layout when Swipe to refresh is used

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_active_prescription, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    initDagger()
    mPresenter.setView(this)

    initializeActivePrescriptionList(pId)


    // Swipe to refresh
    srlActivePrescription.setOnRefreshListener {
        isRefreshRequest = true
        context?.toast("Refreshing")
       mPresenter.getActivePrescriptionLiveData().value?.dataSource?.invalidate()
    }

}

private fun initializeActivePrescriptionList(patientId: Int) {

    mPresenter.getActivePrescriptionLiveData().observe(this, Observer<PagedList<PrescriptionResponse.Data.Result>> { pagedList ->
        activePrescriptionAdapter.submitList(pagedList)

        // Show toast message if the swipe to refresh was used
        if (isRefreshRequest) {
            srlActivePrescription.isRefreshing = false
            context?.toast(getString(R.string.text_refreshed))
            isRefreshRequest = false
        }
    })

    mPresenter.getNetworkState().observe(this, Observer<NetworkState> { networkState ->
        activePrescriptionAdapter.setNetworkState(networkState)
    })
}
}

MyPresenterImpl

class MyPresenterImpl @Inject constructor(var mInteractor: myInteractor) : MyPresenter<MyView> {

var mView: WeakReference<MyView>? = null

override fun setView(view: MyView?) {
    mView = WeakReference<MyView>(view)
}

override fun getNetworkState(): LiveData<NetworkState> {
    return mInteractor.getNetworkState()
}

override fun getActivePrescriptionLiveData(): LiveData<PagedList<PrescriptionResponse.Data.Result>> {
    return mInteractor.getActivePrescriptionLiveData()
}
}

MyInteractorImpl

class MyInteractorImpl(val activePrescriptionService: ActivePrescriptionService,
                                   val networkUtils: NetworkUtils<PrescriptionResponse.Data>,
                                   val networkExecutor: Executor,
                                   val pagingConfig: PagedList.Config)
: MyInteractor {

private var activePrescriptionLiveData: LiveData<PagedList<PrescriptionResponse.Data.Result>>
private var networkStateLiveData: LiveData<NetworkState>

companion object {
    lateinit var activePrescriptionDataSource: ActivePrescriptionDataSource
}

init {
    activePrescriptionDataSource = ActivePrescriptionDataSource(activePrescriptionService, networkUtils, networkExecutor)
    val dataSourceFactory = object : DataSource.Factory<Int, PrescriptionResponse.Data.Result>() {
        override fun create(): DataSource<Int, PrescriptionResponse.Data.Result> {
            return activePrescriptionDataSource
        }
    }
    val pagedListBuilder = LivePagedListBuilder<Int, PrescriptionResponse.Data.Result>(dataSourceFactory, pagingConfig)

    activePrescriptionLiveData = pagedListBuilder.build()
    networkStateLiveData = activePrescriptionDataSource.getNetworkStateLiveData()
}

override fun getNetworkState(): LiveData<NetworkState> {
    return networkStateLiveData
}

override fun getActivePrescriptionLiveData(): LiveData<PagedList<PrescriptionResponse.Data.Result>> {
    return activePrescriptionLiveData
}
}

但问题是,当数据源无效时,什么也没有发生。我需要重新获取滑动中的列表以刷新。任何帮助将不胜感激。

附:我找不到任何将分页库与 MVP 一起使用的示例,因此我自己实现了它。如果我在这里做错了什么,请告诉我。

【问题讨论】:

    标签: android android-architecture-components android-paging android-paging-library


    【解决方案1】:

    您必须始终在工厂 create 方法中创建 DataSource 的新实例:

    val dataSourceFactory = object : DataSource.Factory<Int, PrescriptionResponse.Data.Result>() {
        override fun create(): DataSource<Int, PrescriptionResponse.Data.Result> {
            return ActivePrescriptionDataSource(activePrescriptionService, networkUtils, networkExecutor)
        }
    

    一旦DataSource 失效,它就失效了——你不能再使用它了。这就是为什么你必须在create()中创建新实例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2014-01-16
      相关资源
      最近更新 更多