title: How can I avoid code duplication when getting data from Firebase (android)?
body: <p>I'd imagine this question would apply to any application that uses asynchronous calls. </p>
  
  <p>I'm relatively new to all this, and the problem I'm having is with with fetching data from Firebase to my Android application. </p>
  
  <p>There are several classes throughout the application that need to use data from the "users" table in the database, which I obtain as follows: </p>
  
  <pre><code>DatabaseReference reference = FirebaseDatabase.getInstance().getReference()
  .child("users").child("exampleDataFromUsers");
  reference.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
          // do something with the snapshot
      }
  </code></pre>
  
  <p>I'd like to create a general purpose class for handling data retrieval from Firebase, something like:</p>
  
  <pre><code>public class FirebaseDataHandler() {
  
    static String getUserReports() {
      // get Reports
    }
  
    static int getUserAge() {
      // get age
    }
  }
  </code></pre>
  
  <p>but since it's being retrieved asynchronously, any calls to these methods simply return null. As a result, my code is currently littered with duplicate code. </p>
  
  <p>If anyone has any insight into a better way to achieve this, it would be much appreciated. Thanks. </p>