0

I am using a paging library from Android Architecture Components.

Paging is implemented using ItemKeyedDataSource

class MyDatasource( private val queryMap: HashMap<String, String>) : ItemKeyedDataSource<String, Order>() { private val compositeDisposable: CompositeDisposable by lazy { CompositeDisposable() } override fun loadInitial(params: LoadInitialParams<String>, callback: LoadInitialCallback<Order>) { compositeDisposable.add( MyService.getService().fetchData(queryMap) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(object : DisposableObserver<OrdersResponse>() { override fun onNext(orders: OrdersResponse) { callback.onResult(orders.data) } override fun onError(e: Throwable) { e.printStackTrace() } override fun onComplete() { } }) ) } override fun loadBefore(params: LoadParams<String>, callback: LoadCallback<Order>) { // do nothing } override fun loadAfter(params: LoadParams<String>, callback: LoadCallback<Order>) { queryMap["offsetOrderId"] = params.key compositeDisposable.add( MyService.getService().fetchData(queryMap) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(object : DisposableObserver<OrdersResponse>() { override fun onNext(orders: OrdersResponse) { callback.onResult(orders.data) } override fun onError(e: Throwable) { } override fun onComplete() { } }) ) } override fun getKey(item: Order): String { return item.orderId } } 

I build pagedlist in my viewmodel

class MyViewModel() : ViewModel() { private var myPagingConfig: PagedList.Config? = null var dataList: LiveData<PagedList<Order>>? = null fun getOrders(params: HashMap<String, String>) { if (myPagingConfig == null) { myPagingConfig = PagedList.Config.Builder() .setPageSize(LIMIT) .setPrefetchDistance(10) .setEnablePlaceholders(false) .build() } dataList = LivePagedListBuilder(MyDataFactory( MyDatasource(params)), myPagingConfig!!) .setInitialLoadKey(null) .setFetchExecutor(Executors.newFixedThreadPool(5)) .build() } } 

However, when I observe the dataList in my activity, it sometimes (most of the times) returns an empty list, while in logcat I see that I had fetched data successfully. callback.onResult is invoked after it returns an empty list, but observer never gets notified again.

Can you tell me if what would cause this?

7
  • Have you set the setLifecycleOwner in activity.? Commented Aug 19, 2019 at 7:39
  • No. Why to do that ? Commented Aug 19, 2019 at 7:44
  • Could you please post the activity code. Where you are observing your livedata. Commented Aug 19, 2019 at 7:48
  • Do you receive any error from your observer in loadAfter method? Commented Aug 19, 2019 at 7:53
  • No I don't have any error, and i see the response in my logcat, and callback.onResult also gets called with the right data. but observer not notified Commented Aug 19, 2019 at 7:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.