PageList.size is always zero
Asked Answered
R

1

11

Actually, I am new to Paging Library. Here is situation, i'm observing PagedList from my ViewModel which is always returning zero even news list appeared on UI.

 viewModel.getNews().observe(this, news -> {
                    swipeRefreshLayout.setRefreshing(false);
                    Timber.d("news size is %s",news.size());// **news size is 0**
                    adapter.submitList(news);
                });

My DataSource is

public class NewsDataSource extends ItemKeyedDataSource<String, News> {

    private MutableLiveData<Resource.Status> netStatusLive = new MutableLiveData<>();

   /*Hide Logic for clean purpose*/

    private List<News> localNewsList = new ArrayList<>();

    public NewsDataSource( /*Hide Logic for clean purpose*/) {
         /*Hide Logic for clean purpose*/
    }


    @Override
    public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<News> callback) {
        netStatusLive.postValue(Resource.Status.LOADING);
        localNewsList.clear();
        Disposable disposable =  /*Hide Logic for clean purpose*/

        compositeDisposable.add(disposable);
    }

    @Override
    public void loadAfter(@NonNull LoadParams<String> params, @NonNull LoadCallback<News> callback) {
        if (localNewsList.size() > 19) {
            netStatusLive.postValue(Resource.Status.LOADING);
            Disposable disposable =  /*Hide Logic for clean purpose*/
            compositeDisposable.add(disposable);
        }

    }

    private String getLastIdOfNews(List<News> localNewsList) {
        if (localNewsList.size() <= 0) {
            return "android";
        } else {
            return localNewsList.get(localNewsList.size() - 1).getId();
        }
    }

    @Override
    public void loadBefore(@NonNull LoadParams<String> params, @NonNull LoadCallback<News> callback) {
        //do nothing...
    }

    @NonNull
    @Override
    public String getKey(@NonNull News item) {
        return item.getId();
    }

    private void onPaginationError(Throwable throwable) {
        netStatusLive.postValue(Resource.Status.ERROR);
        Timber.e(throwable);
    }

    public LiveData<Resource.Status> getNetworkState() {
        return netStatusLive;
    }

}

In this case localNewsList is require to give last id of News on loadAfter which is like

Observable<List<News>> getNewsList(@Query("skip") int skip,
                                            @Query("limit") int limit,
                                            @Query("lastNewsId") String lastNewsId);

My DataSourceFactory is

    public class NewsDataSourceFactory extends DataSource.Factory<String, Journal> {
        private MutableLiveData<JournalDataSource> dataSourceLive = new MutableLiveData<>();

         /*Hide Logic for clean purpose*/

        public NewsDataSourceFactory( /*Hide Logic for clean purpose*/) {
             /*Hide Logic for clean purpose*/
        }

        @Override
        public DataSource<String, Journal> create() {
            JournalDataSource dataSource = new JournalDataSource( /*Hide Logic for clean purpose*/);
            dataSourceLive.postValue(dataSource);
            return dataSource;

        }
 public void prepareData(String coursId, String studnetId) {
        this.coursId = coursId;
        this.studnetId = studnetId;
    }


        public MutableLiveData<JournalDataSource> getDataSource() {
            return dataSourceLive;
        }
    }

ViewModle is

public class NewsViewModel extends ViewModel {

    private LiveData<PagedList<News>> newslist = new MutableLiveData<>();
    private LiveData<Resource.Status> networkState = new MutableLiveData<>();

    private NewsDataSourceFactory factory;

    private PagedList.Config config;

    private Executor executor;


    NewsViewModel(NewsDataSourceFactory factory, PagedList.Config config) {
        this.factory = factory;
        this.config = config;

        executor = Executors.newFixedThreadPool(5);


        networkState = Transformations.switchMap(factory.getDataSource(), source -> {
                    Timber.d("network status get");
                    return source.getNetworkState();
                }
        );
    }

    public void getNews(String courseId, String studentId) {
        //Newss = repo.getNewss(auth, courseId, studentId, 0, 20, "android");
        factory.prepareData(courseId, studentId);
        newslist = new LivePagedListBuilder<>(factory, config).setFetchExecutor(executor).build();
    }

    public void refresh() {
        factory.getDataSource().getValue().invalidate();
    }

    public LiveData<PagedList<News>> getNews() {
        return newslist;
    }

    public LiveData<Resource.Status> getNetworkState() {
        return networkState;
    }

I think, my problem may be mismatch my datasource use case and my datasource type.So PageList doesn't work properly as we expected.

Remmer answered 30/10, 2018 at 4:25 Comment(6)
not quite sure but can u please change the %s to %d ... and checkTympanist
Thanks you for answering. I tried.But I thinks it doesn't deal with my situation.Remmer
Can you post your Factory classCallosity
@Ümañgßürmån I updatedRemmer
can you add your ViewModel ?Convolve
@ZwalPyaeKyaw updated!Remmer
B
3

According to Android Paging Library Doc, You should call Api synchronous ! If you use RxRetrofit just remove observeOn() and subscribeOn() to run api on current thread that paging library use . I tested it and it works fine for me .

Brayton answered 26/11, 2018 at 14:38 Comment(2)
Thank you!. It worked. However, It respond only one time to observer( currently viewModel.getNews().observe(this, news -> { Timber.d("news size is %s",news.size());// **news size is 20** //..... });. The things I need is , when i scrolled the list, it should show ,40, 60, and so on ..Remmer
Wow, thanks man. This was causing my recycler to not show results until I touched the screen. I removed observeOn() and subscribeOn() and used an Executor with the LivePagedListBuilder and it worked like charm. PageList is a delicate class to work with.Gilgilba

© 2022 - 2024 — McMap. All rights reserved.