0

I am trying to use Android Paging with ItemKeyedDataSource. Network call fetches list of items but my pagedList is not getting updated with the list.

`override fun loadInitial(
        params: LoadInitialParams<String>,
        callback: LoadInitialCallback<InteractionEntity>
    ) {
        Timber.d("load Initial called")
        interactionsRepository.getInteractions(params.requestedLoadSize,reviewFiltersEntity.locationId,reviewFiltersEntity.startDate,
            reviewFiltersEntity.endDate,reviewFiltersEntity.siteUrls,reviewFiltersEntity.ratingFilters,reviewFiltersEntity.responseStatus,null,null)
            .subscribe(object: Observer<List<InteractionEntity>>{
                override fun onComplete() {
                    Timber.d("getInteractions complete")
                }

                override fun onSubscribe(d: Disposable) {
                    Timber.d("getInteractions subscribed ")
                }

                override fun onNext(t: List<InteractionEntity>) {
                    Timber.d("getInteractions onNext "+t.size)
                    callback.onResult(t)
                }

                override fun onError(e: Throwable) {
                    Timber.d("getInteractions error "+e.message)
                }
            })
    }`

DataSourceFactory

 'var interactionDataSourceLiveData = MutableLiveData<InteractionDataSource>()


override fun create(): DataSource<String, InteractionEntity> {
    val interactionDataSource = InteractionDataSource(interactionsRepository,locationsRepository,reviewFiltersEntity)
    interactionDataSourceLiveData.postValue(interactionDataSource)
    return interactionDataSource
}'

ViewModel

  'fun loadInteractions(reviewFiltersEntity: ReviewFiltersEntity){
         interactionDataSourceFactory = InteractionDataSourceFactory(interactionsRepository,locationsRepository,reviewFiltersEntity)
        interactionDataSourceLiveData = interactionDataSourceFactory?.interactionDataSourceLiveData

        var config = PagedList.Config.Builder()
            .setEnablePlaceholders(true)
            .setInitialLoadSizeHint(10)
            .setPageSize(20)
            .setPrefetchDistance(4)
            .build()

        interactionsPagedList = LivePagedListBuilder<String, InteractionEntity>(interactionDataSourceFactory!!,config)
            .setFetchExecutor(executor)
            .build()
    }'

Activity

'interactionPagingViewModel?.loadInteractions(reviewFiltersEntity)

        interactionPagingViewModel?.interactionsPagedList?.observe(this, Observer {

            Timber.d("interaction paged list changed ")
            if(!it.isNullOrEmpty()){
                Timber.d("fetched interactions in paging "+it.size)
                adapter.submitList(it)

            }else{
                showError(R.string.error_fetching_reviews)
            }

        })'

I can see the list fetched in loadInitial but I do not see the same onChanged of Observer above.

Any idea What could be wrong?

mansi rao
  • 396
  • 4
  • 3

1 Answers1

0

Found the solution. The problem was DataSource of Android Paging required the api calls to be synchronous. Since the thread of data source and that of api call were different the data source was unable to post the result fetched by api call. So Instead of subscribe, I called blockingFirst() and it worked.

mansi rao
  • 396
  • 4
  • 3