title: How to constantly update query for Firebase Realtime Database with LiveData and ViewModel in Android
body: <p>I use Firebase Realtime Database and LiveData with ViewModel in Android and I would like to constantly update the query.</p>
  <p>Here is the ViewModel class:</p>
  <pre><code>public class ViewModel_MainActivity extends ViewModel {
  
  
      /*
      LiveData with Database query for the Firebase node &quot;Ratings&quot;
      */
  
      private static long currentTimeMillisRating = System.currentTimeMillis();
      private static double pastMinuteForDisplayingRatings = 1;
      private static long pastTimeMillisRatings = System.currentTimeMillis() - (long) (pastMinuteForDisplayingRatings * 60 * 60 * 1000);
  
  
      private static final Query QUERY_RATINGS =
              FirebaseDatabase.getInstance(DB_SQLite_Firebase_Entries.FIREBASE_URL).getReference().child(DB_SQLite_Firebase_Entries.FIREBASE_NODE_RATINGS).orderByChild(DB_SQLite_Firebase_Entries.FIREBASE_RATINGDATEINMILLISECONDS).endAt(pastTimeMillisRatings);
  
  
      private final LiveData_FirebaseRating liveData_firebaseRating = new LiveData_FirebaseRating(QUERY_RATINGS);
  
      @NonNull
      public LiveData_FirebaseRating getDataSnapshotLiveData_FirebaseRating() {
          Log.e(&quot;LogTag_ViMo&quot;, &quot;pastTimeMillisRatings: &quot; + pastTimeMillisRatings);
          return liveData_firebaseRating;
      }
  
  
  }
  </code></pre>
  <p>Here is the LiveData class:</p>
  <pre><code>public class LiveData_FirebaseRating extends LiveData&lt;DataSnapshot&gt; {
      private static final String LOG_TAG = &quot;LiveData_FirebaseRating&quot;;
  
      private Query query;
      private final LiveData_FirebaseRating.MyValueEventListener listener = new LiveData_FirebaseRating.MyValueEventListener();
  
      DataSnapshot currentDataSnapShotFromFirebase;
  
      public LiveData_FirebaseRating(Query query) {
          this.query = query;
      }
  
      public LiveData_FirebaseRating(DatabaseReference ref) {
          this.query = ref;
      }
  
      public void changeQuery(Query newQuery) {
          this.query = newQuery;
      }
  
      public DataSnapshot getCurrentDataSnapShotFromFirebase() {
          return currentDataSnapShotFromFirebase;
      }
  
      public void setCurrentDataSnapShotFromFirebase(DataSnapshot currentDataSnapShotFromFirebase) {
          this.currentDataSnapShotFromFirebase = currentDataSnapShotFromFirebase;
      }
  
      @Override
      protected void onActive() {
          Log.d(LOG_TAG, &quot;onActive&quot;);
          query.addValueEventListener(listener);
      }
  
      @Override
      protected void onInactive() {
          Log.d(LOG_TAG, &quot;onInactive&quot;);
          query.removeEventListener(listener);
      }
  
      private class MyValueEventListener implements ValueEventListener {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
  
  
              setValue(dataSnapshot);
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
              Log.e(LOG_TAG, &quot;Can't listen to query &quot; + query, databaseError.toException());
          }
      }
  }
  </code></pre>
  <p>And here is the part of the main activity, where the live data and view model are created and observed:</p>
  <pre><code>@Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
  
  
          binding = ActivityMainBinding.inflate(getLayoutInflater());
          View view = binding.getRoot();
          setContentView(view);
         
          /*
          Initiate View Model with LiveData for Firebase
           */
  
  
  
          rootRef_Firebase = FirebaseDatabase.getInstance(DB_SQLite_Firebase_Entries.FIREBASE_URL).getReference();
          viewModel = new ViewModelProvider(this).get(ViewModel_MainActivity.class);
  
          liveData_firebaseRating = viewModel.getDataSnapshotLiveData_FirebaseRating();
          liveData_firebaseRating.observe(this, new Observer&lt;DataSnapshot&gt;() {
  
              @Override
              public void onChanged(@Nullable DataSnapshot dataSnapshot) {
                  liveData_firebaseRating.setCurrentDataSnapShotFromFirebase(dataSnapshot);
                  if(liveData_firebaseRating.getCurrentDataSnapShotFromFirebase()!=null) {
                      //Do something with the dataSnapshot of the query
                  }
              }// end onChange
          }); //end observe
  
  
  
      }
  </code></pre>
  <p>So my problem lies in the non-updating of the <code>QUERY_RATINGS </code>in the class <code>ViewModel_MainActivity </code>. What I want is to return all the nodes from the <code>FIREBASE_NODE_RATINGS</code> whose at attribute <code>FIREBASE_RATINGDATEINMILLISECONDS</code> is older than 1 Minute (meaning that the entry was created at least 1 Minute before the current time slot). How can I do that such that I always get these nodes?</p>