title: How to include Source Cache in cloud firestore realtime update in MVVM architecture android
body: <p>In my app i am using android MVVM architecture, so for retrieving  data from cloud firestore i am using layers so i create one more class (<strong>FirebaseQueryLiveData</strong>) for getting result from firestore. So with my code i am getting the realtime update but not able to add the <strong>Cache Source</strong> feature of firestore.I want to enable offline mode by adding cache soure. How to add it. </p>
  
  <p><strong>ProductViewModel.java</strong></p>
  
  <pre><code>public class ProductViewModel extends AndroidViewModel {
  private FirebaseFirestore db = FirebaseFirestore.getInstance();
  private MediatorLiveData&lt;List&lt;ProductModel&gt;&gt; productListLiveData;
  private FirebaseQueryLiveData liveData;
   public ProductViewModel(@NonNull Application application) {
          super(application);
      }
  public LiveData&lt;List&lt;ProductModel&gt;&gt; getProductList() {
          productListLiveData = new MediatorLiveData&lt;&gt;();
          completeProductList();
          return productListLiveData;
      }
        private void completeProductList() {
       Query query =  db.collection("mainCollection").document("productList")
                  .collection("productCollection");
          liveData = new FirebaseQueryLiveData(query);
          productListLiveData.addSource(liveData, new Observer&lt;QuerySnapshot&gt;() {
              @Override
              public void onChanged(QuerySnapshot queryDocumentSnapshots) {
                  if (queryDocumentSnapshots!= null){
                      List&lt;ProductModel&gt; productModelList = new ArrayList&lt;&gt;();
                      for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){
                          ProductModel model = documentSnapshot.toObject(ProductModel.class);
                          productModelList.add(model);                     
                      }productListLiveData.setValue(productModelList);
                  }
              }
          });
  
  }
  </code></pre>
  
  <p><strong>FirebaseQueryLiveData.java</strong></p>
  
  <pre><code>public class FirebaseQueryLiveData extends LiveData&lt;QuerySnapshot&gt; {
  
      private MyValueEventListener listener = new MyValueEventListener();
      private Query query;
      Source source = Source.CACHE;
  
      private boolean listenerRemovePending = false;
      private ListenerRegistration registration;
      private Handler handler = new Handler();
      private final Runnable removeListener = new Runnable() {
          @Override
          public void run() {
              registration.remove();
              listenerRemovePending = false;
  
          }
      };
  
      public FirebaseQueryLiveData(Query query) {
          this.query = query;
      }
  
      @Override
      protected void onActive() {
          super.onActive();
          if (listenerRemovePending){
              handler.removeCallbacks(removeListener);
          }else {
            registration=  query.addSnapshotListener(listener);
          }
          listenerRemovePending= false;
      }
  
      @Override
      protected void onInactive() {
          super.onInactive();
          handler.postDelayed(removeListener, 2000);
          listenerRemovePending=true;
      }
  
      private class MyValueEventListener implements EventListener&lt;QuerySnapshot&gt; {
          @Override
          public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
              setValue(queryDocumentSnapshots);
          }
      }
  }
  </code></pre>