Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PageList.size is always zero

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.

like image 537
amlwin Avatar asked Oct 30 '18 04:10

amlwin


1 Answers

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 .

like image 180
Keyvan Norouzi Avatar answered Oct 14 '22 03:10

Keyvan Norouzi