了解Firebase何时完成API调用? [英] Knowing when Firebase has completed API call?

查看:134
本文介绍了了解Firebase何时完成API调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

知道我的Firebase API调用何时完成,我遇到了一些麻烦。阅读 Firebase文档后,我发现以下内容:
$ b


值事件总是最后触发,并保证包含在拍摄快照之前发生的任何其他事件的更新。


我明白这只是说在完成 onChildAdded 调用之后,调用 ValueEventListener 。因此,我认为可以在 onChildAdded 函数中填充我的 RecyclerView ,然后填充 onSingleValueListener 调用,我可以简单地完成动画我的加载屏幕(这已经开始动画在这个函数调用之前)并继续。然而,我遇到了一个问题,我把一些小心 System.out.println 语句,发现在我的情况下, Test 1 Test 2 之前调用c $ c>。这会导致问题,因为这实际上是我想要的相反的行为:我希望 onChildAdded 函数完成,然后调用 onSingleValueListener 函数打印出 Test 1 被调用。这有什么道理吗?有什么办法呢?我将不胜感激解释为什么发生这种情况。

preublic $ getComments(final String postId,final Activity activity,final View fragmentView,final View progressOverlay){
最终Firebase commentsRef = firebaseRef.child(/ comments);
Firebase linkRef = firebaseRef.child(/ posts /+ postId);
linkRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ 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);
}
}
$ b @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){


})

$ b @Override $ b $公共无效onChildChanged(DataSnapsh ot dataSnapshot,String s){

}
$ b $ @覆盖
public void onChildRemoved(DataSnapshot dataSnapshot){

}

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


$ @覆盖
public void onCancelled(FirebaseError firebaseError){

}
});


$解析方案 $ c>Firebase linkRef = firebaseRef.child(/ posts /+ postId);我可以看到您使用的是旧版Firebase API。 现已弃用!



请将您的代码更新为新的Firebase 3.xx 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 Activity activity,final View fragmentView,final View progressOverlay){
DatabaseReference commentsRef = firebaseRef.child(/ comments) ;
DatabaseReference linkRef = commentsRef.child(/ posts /+ postId);
linkRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
//遍历数据快照并更新Adapter数据集$ b (DataSnapshot snapshot:dataSnapshot.getChildren()){
Comment comment = snapshot.getValue(Comment.class);
application.getCommentsRecyclerViewAdapter()。getCommentsList()。add(comment);

application.getCommentsRecyclerViewAdapter()。notifyDataSetChanged();

//关闭加载进度条
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);
}
}
$ b @覆盖
public void onCancelled(DatabaseError databaseError){
//处理失败事件在这里
}
});

希望这会对您有所帮助!


I am having some trouble knowing when my Firebase API call is finished. After reading the Firebase documentation I have found the following:

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

I understand this to mean that only after all the onChildAdded call is finished, then the ValueEventListener is called. As a result, I thought that I can populate my RecyclerView in the onChildAdded function and then the onSingleValueListener 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 System.out.println statements and found that in my case, Test 1 is called before Test 2 is ever called. This causes problems because this is actually the opposite behavior of what I wanted: I wanted the onChildAdded function to finish and then call the onSingleValueListener function that prints out Test 1 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!

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) {

        }
    });
} 

解决方案

With this code "Firebase linkRef = firebaseRef.child("/posts/" + postId);" I could see that you're using legacy Firebase API. Its deprecated now!

Kindly update your code to new Firebase 3.x.x API.

Below two are independent async call; Based on your use-case, you can use either one of the listener to read your data.

1. linkRef.addListenerForSingleValueEvent(new ValueEventListener() {});
2. linkRef.addChildEventListener(new ChildEventListener() {});

You can refer the firebase document to get more information about database listeners. https://firebase.google.com/docs/database/android/retrieve-data

With the following code snippet, you can retrieve and populate your list of comments.

public void getComments(final String postId, final Activity activity, final View fragmentView, 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, View.GONE, 0, 200);
                fragmentView.findViewById(R.id.rv_view_comments).setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Handle fail case here
        }
    });

Hope this would help you!

这篇关于了解Firebase何时完成API调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆