title: Firebase setOffline() and reading offline data (android)
body: <p>Following situation:</p>
  
  <ol>
  <li>User not registered, I save data while offline (Firebase setOffline())</li>
  <li>Cannot read the local data (populate listview etc) - the <em>ValueEventListener</em> and <em>ChildEventListener</em> <strong>dont fire</strong></li>
  <li>I set setOnline() on Firebase instance</li>
  <li>Data is synced with web and displayed (listeners fire)</li>
  <li>I set setOffline() again. </li>
  <li>I save local data and read local data, works (listeners fire)</li>
  </ol>
  
  <p>Question:</p>
  
  <p><strong>How to read local data stored BEFORE going online?</strong></p>
  
  <p>Scenario is: User uses the android app offline and decides later to register</p>
  
  <p>Scenario 1:</p>
  
  <pre><code>FirebaseDatabase.getInstance().setPersistenceEnabled(true);
  FirebaseDatabase.getInstance().goOffline();  // &lt;--------NOTE THIS
  
  DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
  mDatabase.child("users").child(App.get().getUid()).child("items").addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
          // THIS IS NOT FIRING
      }
  
      @Override
      public void onCancelled(DatabaseError databaseError) {
          ...
      }
  });
  </code></pre>
  
  <p>After Scenario 1 I change code to this and run:</p>
  
  <pre><code>FirebaseDatabase.getInstance().setPersistenceEnabled(true);
  FirebaseDatabase.getInstance().goOnline();  // &lt;--------NOTE THIS
  
  DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
  mDatabase.child("users").child(App.get().getUid()).child("items").addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
          // THIS IS FIRING. ALL GOOD
      }
  
      @Override
      public void onCancelled(DatabaseError databaseError) {
          ...
      }
  });
  </code></pre>
  
  <p>After this I change code to following and it works</p>
  
  <pre><code>FirebaseDatabase.getInstance().setPersistenceEnabled(true);
  FirebaseDatabase.getInstance().goOffline(); // &lt;--------NOTE THIS
  
  DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
  mDatabase.child("users").child(App.get().getUid()).child("items").addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
          // THIS IS FIRING. ALL GOOD
      }
  
      @Override
      public void onCancelled(DatabaseError databaseError) {
          ...
      }
  });
  </code></pre>
  
  <p>I added 3 segments (code blocks).</p>
  
  <ul>
  <li>I execute first block - does not work</li>
  <li>Second block - works</li>
  <li>Third block - works</li>
  </ul>
  
  <p>Here is a <a href="https://gist.github.com/ramden/3addea21cf925ecd3c8c380f78679ad9" rel="nofollow noreferrer">gist with the code</a>.</p>
  
  <p>Problem is that first block does not work before being online with setOnline()</p>