Knowing when Firebase has completed API call?
<p>I am having some trouble knowing when my Firebase API call is finished. After reading the <code>Firebase documentation</code> I have found the following: </p>

<blockquote>
  <p>Value events are always triggered last and are guaranteed to contain updates from any other events which occurred before that snapshot was taken.</p>
</blockquote>

<p>I understand this to mean that only after all the <code>onChildAdded</code> call is finished, then the <code>ValueEventListener</code> is called. As a result, I thought that I can populate my <code>RecyclerView</code> in the <code>onChildAdded</code> function and then the <code>onSingleValueListener</code> call, I can simply finish animating my loading screen (which has started animating before this function call) and proceed. However, I have run into an issue where I put some careful <code>System.out.println</code> statements and found that in my case, <code>Test 1</code> is called before <code>Test 2</code> is ever called. This causes problems because this is actually the opposite behavior of what I wanted: I wanted the <code>onChildAdded</code> function to finish and then call the <code>onSingleValueListener</code> function that prints out <code>Test 1</code> to be called. Is there any reason why this is happening? Any way around this? I would appreciate an explanation on why this is happening. Thanks!</p>

<pre><code>public void getComments(final String postId, final Activity activity, final View fragmentView, final View progressOverlay) {
    final Firebase commentsRef = firebaseRef.child("/comments");
    Firebase linkRef = firebaseRef.child("/posts/" + postId);
    linkRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            System.out.println("Test 1");
            if (progressOverlay.getVisibility() == View.VISIBLE) {
                progressOverlay.setVisibility(View.GONE);
                AndroidUtils.animateView(progressOverlay, View.GONE, 0, 200);
                fragmentView.findViewById(R.id.rv_view_comments).setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    linkRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            commentsRef.child(dataSnapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Comment comment = dataSnapshot.getValue(Comment.class);
                    System.out.println("Test 2");
                    application.getCommentsRecyclerViewAdapter().getCommentsList().add(comment);
                    application.getCommentsRecyclerViewAdapter().notifyDataSetChanged();
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
} 
</code></pre>