当我的Firebase API调用完成时,我有些麻烦.阅读Firebase文档后,我发现以下内容:
Value events are always triggered last and are guaranteed to contain updates from any other events which occurred before that snapshot was taken.
我理解这一点意味着只有在所有的onChildAdded调用完成之后,才会调用ValueEventListener.因此,我想我可以在onChildAdded函数中填充RecyclerView,然后在onSingleValueListener调用中填充我的RecyclerView,我可以简单地完成动画加载屏幕(已经开始在此函数调用之前进行动画化)并继续.但是,我遇到了一个问题,在这里我放了一些细节的System.out.println语句,发现在我的情况下,测试1被调用之前测试2被调用.这会导致问题,因为这实际上是我想要的相反的行为:我想要onChildAdded函数完成,然后调用打印出Test 1被调用的onSingleValueListener函数.有什么原因造成这种情况吗?这样吗?我会很乐意解释为什么会发生这种情况.谢谢!
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,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) { } }); }
解决方法
使用此代码“Firebase linkRef = firebaseRef.child(”/ posts /“postId);”我可以看到您使用的是旧版Firebase API.现在已经弃用了
请将您的代码更新为新的Firebase 3.x.x API.
下面两个是独立的异步调用;根据用例,您可以使用其中一个侦听器来读取数据.
1. linkRef.addListenerForSingleValueEvent(new ValueEventListener() {}); 2. linkRef.addChildEventListener(new ChildEventListener() {});
您可以参考firebase文档来获取有关数据库侦听器的更多信息.
https://firebase.google.com/docs/database/android/retrieve-data
使用以下代码段,您可以检索并填充您的注释列表.
public void getComments(final String postId,final View progressOverlay) { DatabaseReference commentsRef = firebaseRef.child("/comments"); DatabaseReference linkRef = commentsRef.child("/posts/" + postId); linkRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // Iterate through data-snapshot,and update your Adapter dataset for (DataSnapshot snapshot : dataSnapshot.getChildren()) { Comment comment = snapshot.getValue(Comment.class); application.getCommentsRecyclerViewAdapter().getCommentsList().add(comment); } application.getCommentsRecyclerViewAdapter().notifyDataSetChanged(); // Dismiss your loading progressbar if (progressOverlay.getVisibility() == View.VISIBLE) { progressOverlay.setVisibility(View.GONE); AndroidUtils.animateView(progressOverlay,200); fragmentView.findViewById(R.id.rv_view_comments).setVisibility(View.VISIBLE); } } @Override public void onCancelled(DatabaseError databaseError) { // Handle fail case here } });
希望这会帮助你!