title: Firebase exception handling around .setValue()
body: <p>The below code inserts data in the Firebase database and runs fine when my Android device has network connectivity. However, if I'm offline it does not sync directly with the online database. I would like to be notified if it's being cached rather than being synced directly.</p>
  
  <pre><code>DatabaseReference databaseIssue;
  databaseIssue = FirebaseDatabase.getInstance().getReference("Issue");
  
  String timeDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
  
  databaseIssue.child(timeDate).setValue("test");
  </code></pre>
  
  <p>Is there anyway to implement somthing like simple try/catch around:</p>
  
  <pre><code>databaseIssue.child(timeDate).setValue("test");
  </code></pre>
  
  <p>A catch if the data is only cached? </p>
  
  <p>From the Firebase documentation (<a href="https://firebase.google.com/docs/database/admin/save-data" rel="nofollow noreferrer">https://firebase.google.com/docs/database/admin/save-data</a>)</p>
  
  <p><strong>Adding a Completion Callback</strong></p>
  
  <pre><code>DatabaseReference dataRef = ref.child("data");
  dataRef.setValueAsync("I'm writing data", new DatabaseReference.CompletionListener() {
    @Override
    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
      if (databaseError != null) {
        System.out.println("Data could not be saved " + databaseError.getMessage());
      } else {
        System.out.println("Data saved successfully.");
      }
    }
  });
  </code></pre>
  
  <p>The set value does throw other exceptions:
  <a href="https://stackoverflow.com/questions/44804661/firebase-permission-denied-setvalue">Firebase: Permission denied - setValue()</a></p>
  
  <p>Does firebase throw an exception if it cannot connect to the online database and has to cache the data? Is there a way to log, if the data is being cached or synced?</p>
  
  <p><strong>Network Connectivity and Offline Writes</strong> </p>
  
  <blockquote>
    <p>Firebase Node.js and Java clients maintain their own internal version of >any active data. When
    data is written, it is written to this local version first. The client
    then synchronizes that data with the database and with other clients
    on a 'best-effort' basis.</p>
    
    <p>As a result, all writes to the database will trigger local events
    immediately, before any data has even been written to the database.
    This means that when you write an application using Firebase, your app
    will remain responsive regardless of network latency or Internet
    connectivity.</p>
    
    <p>Once connectivity is reestablished, we'll receive the appropriate set
    of events so that the client "catches up" with the current server
    state, without having to write any custom code.</p>
  </blockquote>