Firebase Database Android Issues
<p>I have recently been working with Android development. I have been developing a social networking app. For the app, I decided to create a separate helper class for all database methods. In my database, all users have a user id and their information is stored under this id. I have a (non-static) method in this class that would get certain User information when given a DatabaseReference to the user's information location. The method would simply take the reference, add a listener for single value event (addListenerForSingleValueEvent(ValueEventListener)). I was encountering problems with this so I tried putting a Log statement in the onDataChange() method of the ValueEventListener. Oddly enough, this Log method was never reached. Even more strange is the fact that, if I copy and paste the code from this method into one of the locations where I need it, the Log statement is reached. Does anyone have any idea as to why this happens? This is a method that I am using in multiple activities and copying and pasting the code everywhere would make the code very sloppy. Any help would be much appreciated. Thank you!</p>

<p><strong>Update:</strong> It turns out the code works if placed in the Database class, but the Log statement will only run after the method is over. Below is the  an outline of the class I am using to observe this.</p>

<p><strong>Fragment Class</strong></p>

<pre><code>public class FragmentClass extends Fragment {
    public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      mDatabase = DatabaseManager.getInstance();
      String userId = "userId";

      mDatabase.getUserFromUserId(userId); 
    }
}
</code></pre>

<p><strong>Database Class</strong></p>

<pre><code>public class DatabaseManager {
    private static FirebaseDatabaseManager mInstance;

    private static FirebaseDatabase mDatabase;

    public static FirebaseDatabaseManager getInstance() {
      if(mInstance == null) {
        mInstance = new FirebaseDatabaseManager();
      }

      return mInstance;
    }

    private FirebaseDatabaseManager() {
      mDatabase = FirebaseDatabase.getInstance();
    }

    public void getUserFromUserId(final String userId) {
      DatabaseReference userReference = mDatabase.getReference(userId);

      userReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
          Log.i("databaseTag", "reached");
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
          Log.i("databaseTag", "reached");
        }
    });

    while(true) { // if this part is commented out, the log statement will be executed; otherwise, it won't
    }
   }
 }
</code></pre>