【问题标题】:How to observe PagedList LiveData as it submit result asynchronously?如何在异步提交结果时观察 PagedList LiveData?
【发布时间】:2019-12-20 07:14:32
【问题描述】:

在下面的代码 sn-p 中,PagedList LiveData 只观察了一次。它成功地将下一页加载到recyclerView。正如我们所知,它在内部使用异步调用将结果提交给适配器。

问题是如何观察每个插入到列表中的新数据的 PagedList LiveData?

val callback = PostListAdapter.PagerCallback()

viewModel.posts.observe(viewLifecycleOwner, Observer<PagedList<PostData>> { pagedList ->
  adapter.submitList(pagedList) // 
  adapter.updatePostList(pagedList) // I want to update this list on new data

  pagedList.addWeakCallback(null, callback) // callback working fine.
}

我也尝试了 PagedList.Callback(),它工作正常但没有观察到 LiveData

class PagedCallback() : PagedList.Callback() {
    override fun onChanged(position: Int, count: Int) {}

    override fun onInserted(position: Int, count: Int) {
        println("count: $count")
    }
    override fun onRemoved(position: Int, count: Int) {}
})

【问题讨论】:

    标签: android kotlin android-livedata android-paging


    【解决方案1】:

    我找到了适合我的方案的解决方案并分享了相同的解决方案。

    第一次调用后我们无法观察 PagedList LiveData,因为它异步将结果提交给适配器,这导致实时数据不会触发。

    解决方案:我在 DataSource 类中添加了新的 LiveData 并在收到服务器的响应后对其进行了更新。

    视图模型

    viewModel.posts.observe(viewLifecycleOwner, Observer { pagedList ->
        adapter.submitList(pagedList)
    })
    
    // Added new LiveData to observer my list and pass it to detail screen
    viewModel.postList.observe(viewLifecycleOwner, Observer {
        adapter.updatePostList(it)
    })
    

    存储库

    class MyRepository(private val api: ApiService) : {
    val sourceFactory = MyDataSourceFactory(api) // create MyDataSource
    val postList = Transformations.switchMap( // this is new live data to observe list
           sourceFactory.sourceLiveData) { it.postList }
    
    val data = LivePagedListBuilder(sourceFactory, PAGE_SIZE).build() 
    
    return Result(data, postList)
    }
    

    数据源工厂

    class MyDataSourceFactory(private val api: ApiService) : 
        DataSource.Factory<String, PostData>() {
    
    val sourceLiveData = MutableLiveData<MyDataSource>()
    
    override fun create(): DataSource<String, PostData> {
       val source = MyDataSource(api)
       sourceLiveData.postValue(source)
       return source
     }
    }
    

    数据源

    class MyDataSource(private val api: ApiService)
        :PageKeyedDataSource<String, PostData>() {
    
     val postList = MutableLiveData<List<PostData>>()
    
     override fun loadInitial(
         params: LoadInitialParams<String>,
         callback: LoadInitialCallback<String, PostData>) {
         //Api call here
         val list = response.body()?.data
         callback.onResult( // this line submit list to PagedList asynchronously
           list,
           response.body()?.data?.before,
           response.body()?.data?.after) 
    
         postList.value = response.body().data // your LiveData to observe list
    }
    
     override fun loadAfter(
         params: LoadParams<String>,
         callback: LoadCallback<String, PostData>) {
         //Api call here
         val list = response.body()?.data?
         callback.onResult( // this line submit list to PagedList asynchronously
           list,
           response.body()?.data?.before,
           response.body()?.data?.after) 
    
         postList.value = response.body().data // your LiveData to observe list
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      相关资源
      最近更新 更多