title: How to bypass the Firebase cache to refresh data (in Android app)?
body: <p>On an Android application which must works offline most of the time I need, when it's online, to do some synchronous operations for i.e. :</p>
  
  <pre><code>User myUser =  MyclientFacade.getUser();
  If (myUser.getScore &gt; 10) {
      DoSomething() 
  }
  </code></pre>
  
  <p>Where User is a POJO filled by Firebase;</p>
  
  <p>The problem occurs when the Firebase cache is activated </p>
  
  <pre><code>Firebase.getDefaultConfig().setPersistenceEnabled(true);
  </code></pre>
  
  <p>and the user is already in cache and the data are updated on Firebase DB by a third party (or even by another device). Indeed when I query Firebase to get the User I obtain first the data from the cache and later a second change event with the latest data from the Firebase server, but it's too late!</p>
  
  <p>Let's see the synchronous method MyclientFacade.getUser() :</p>
  
  <pre><code>Public User  getUser()  {
    Firebase ref = myFireBaseroot.child("User").child(uid);
    ref.keepSynced(true);
    /* try {
      Thread.sleep(3000);
   } catch (InterruptedException e) {
      e.printStackTrace();
   }*/
  final CountDownLatch signal = new CountDownLatch(1);
  
  ref.addListenerForSingleValueEvent(new ValueEventListener() {
  //ref.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
         this.user = dataSnapshot.getValue(User.class);
         signal.countDown();
      }
      @Override
      public void onCancelled(FirebaseError firebaseError) {
         signal.countDown();
      }
  });
  signal.await(5, TimeUnit.SECONDS);
  ref.keepSynced(false);
  return this.user;
  }
  </code></pre>
  
  <p>I obtain the same behavior if I use <code>addValueEventListener</code> or <code>addListenerForSingleValueEvent</code> mixed with <code>ref.keepSynced</code>:</p>
  
  <p>Let's say my user's score value in cache is 5 and from Firebase DB is 11.</p>
  
  <p>When I call <code>getUser</code> I will obtain the score of 5 (Firebase ask cache first) so I will not call the <code>doSomething()</code> method.</p>
  
  <p>If I uncomment the <code>Thread.sleep()</code> code from my example, the Firebase cache will have enough time to be updated and my <code>getUser</code> will return the correct score value (11).</p>
  
  <p><strong>So how can I directly ask the latest value directly from server side and bypass the cache?</strong> </p>