title: Paging library - populate from cache while requesting from network
body: <p>I have to load cached version of data from database and simultaneously I want to make a request to server for fresh data and I want to do this on per page basis. 
  So, for example for first page I want to show a cached version of first page data from database while requesting fresh data only for first page.
  <br/>
  I want to achieve this using <a href="https://developer.android.com/topic/libraries/architecture/paging.html" rel="nofollow noreferrer">Paging Library</a>.
  <br>
  I tried creating custom data source which helped me intercept page load request which then I used to make a network call with required page number and limit and meanwhile I returned a cached version from db, the problem is after getting fresh data from network I update the database but those updates are not reflected.
  <br>
  (I believe the whole table is being observed for any modifications using Invalidation Tracker and data source is invalidated whenever tables are invalidated, I added that tracker in my data source too but still it ain't working; I was able to make out that Invalidation Tracker thing by temporarily creating: LivePagedListProvider getJobs() in JobDao and checking generated implementation)</p>
  
  <p>Code:</p>
  
  <pre><code>public class JobListDataSource&lt;T&gt; extends TiledDataSource&lt;T&gt; {
  
  private final JobsRepository mJobsRepository;
  private final InvalidationTracker.Observer mObserver;
  
  
  String query = "";
  
  public JobListDataSource(JobsRepository jobsRepository) {
      mJobsRepository = jobsRepository;
  
      mObserver = new InvalidationTracker.Observer(JobEntity.TABLE_NAME) {
          @Override
          public void onInvalidated(@NonNull Set&lt;String&gt; tables) {
              invalidate();
          }
      };
  
      jobsRepository.addInvalidationTracker(mObserver);
  }
  
  
  @Override
  public int countItems() {
      return DataSource.COUNT_UNDEFINED;
  }
  
  @Override
  public List&lt;T&gt; loadRange(int startPosition, int count) {
      return (List&lt;T&gt;) mJobsRepository.getJobs(query, startPosition, count);
  }
  
  
  public void setQuery(String query) {
      this.query = query;
  }
  }
  </code></pre>
  
  <p>Jobs repository functions:</p>
  
  <pre><code>public List&lt;JobEntity&gt; getJobs(String query, int startPosition, int count) {
      if (!isJobListInit) {
          JobList jobList = mApiService.getOpenJobList(
                  mRequestJobList.setPageNo(startPosition/count + 1)
                  .setMaxResults(count)
                  .setSearchKeyword(query)
          ).blockingSingle();
          mJobDao.insert(jobList.getJobsData());
      }
      return mJobDao.getJobs(startPosition, count);
  }
  
  public void addInvalidationTracker(InvalidationTracker.Observer observer) {
      mAppDatabase.getInvalidationTracker().addObserver(observer);
  }
  </code></pre>