Skip to content Skip to sidebar Skip to footer

Paging Library Invalidating Data Source Not Working

Recently I was trying this: I have a list of jobs backed by data source (I am using paging library) and each item in job list is having a save button, and that save button updates

Solution 1:

After reading the source code of DataSource I realized this:

  1. A DataSource once invalidated will never become valid again.
  2. invalidate() says: If invalidate has already been called, this method does nothing.

I was actually having a singleton of my custom DataSource (JobListDataSource) provided by JobsPagedListProvider, so when I was invalidating my DataSource in saveJob() (defined in JobsRepository), it was trying to get new DataSource instance (to fetch latest data by again calling loadRange() - that's how refreshing a DataSource works) but since my DataSource was singleton and it was already invalid so no loadRange() query was being made!

So make sure you don't have a singleton of DataSource and invalidate your DataSource either manually (by calling invalidate()) or have a InvalidationTracker in your DataSource's constructor.

So the final solution goes like this:

Don't have a singleton in JobsPagedListProvider:

publicclassJobsPagedListProvider {

privateJobListDataSource<JobListItemEntity> mJobListDataSource;

private final JobsRepository mJobsRepository;

publicJobsPagedListProvider(JobsRepository jobsRepository) {
    mJobsRepository = jobsRepository;
}

publicLivePagedListProvider<Integer, JobListItemEntity> jobList() {
    returnnewLivePagedListProvider<Integer, JobListItemEntity>() {
        @OverrideprotectedDataSource<Integer, JobListItemEntity> createDataSource() {
            //always return a new instance, because if DataSource gets invalidated a new instance will be required(that's how refreshing a DataSource works)          
            mJobListDataSource = newJobListDataSource<>(mJobsRepository);
            return mJobListDataSource;
        }
    };
}

publicvoidsetQueryFilter(String query) {
    mJobListDataSource.setQuery(query);
}
}

Also make sure if you're fetching data from network you need to have right logic to check whether data is stale before querying the network else it will requery everytime the DataSource gets invalidated. I solved it by having a insertedAt field in JobEntity which keeps track of when this item was inserted in DB and checking if it is stale in getJobs() of JobsRepository. Here is the code for getJobs():

publicList<JobListItemEntity> getJobs(String query, int startPosition, int count) {
    Observable<JobList> jobListObservable = mApiService.getOpenJobList(
            mRequestJobList.setPageNo(startPosition / count + 1)
                    .setMaxResults(count)
                    .setSearchKeyword(query));

    List<JobListItemEntity> jobs = mJobDao.getJobsLimitOffset(count, startPosition);

    //no data in db, make a synchronous call to network to get the dataif (jobs.size() == 0) {
        JobList jobList = jobListObservable.blockingSingle();
        updateJobList(jobList, startPosition, false);
    } elseif (shouldRefetchJobList(jobs)) {
        //data available in db, so show a cached version and make async network call to update data
        jobListObservable.subscribe(newObserver<JobList>() {
            @OverridepublicvoidonSubscribe(Disposable d) {

            }

            @OverridepublicvoidonNext(JobList jobList) {
                updateJobList(jobList, startPosition, true);
            }

            @OverridepublicvoidonError(Throwable e) {
                Timber.e(e);
            }

            @OverridepublicvoidonComplete() {

            }
        });
    }

    return mJobDao.getJobsLimitOffset(count, startPosition);
}

Finally remove InvalidationTracker in JobListDatasource as we are handling invalidation manually:

publicclassJobListDataSource<T> extendsTiledDataSource<T> {

privatefinal JobsRepository mJobsRepository;

Stringquery="";

publicJobListDataSource(JobsRepository jobsRepository) {
    mJobsRepository = jobsRepository;
    mJobsRepository.setJobListDataSource(this);
}

@OverridepublicintcountItems() {
    return DataSource.COUNT_UNDEFINED;
}

@Overridepublic List<T> loadRange(int startPosition, int count) {
    return (List<T>) mJobsRepository.getJobs(query, startPosition, count);
}


publicvoidsetQuery(String query) {
    this.query = query;
}
}

Post a Comment for "Paging Library Invalidating Data Source Not Working"