找出firebase上的子事件侦听器是否完全加载所有数据 [英] find out if child event listener on firebase completely load all data

查看:94
本文介绍了找出firebase上的子事件侦听器是否完全加载所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用firebase存储和检索我的Android应用程序的数据,在我的活动中,我使用childEventListener从firebase检索所有数据(例如:列出用户数据)。



我的问题是:



只要数据没有完全检索,我想显示一个进度条从firebase使用childEventListener。如何检查所有数据是否完全检索?因为我想在数据完全检索后关闭进度条。



任何答案都不胜感激,谢谢!

解决方案

有一种常见的方法来检测Firebase是否完成同步指定位置的初始化数据。此方法利用 Firebase事件保证之一


值事件始终始终触发,并保证包含在拍摄快照之前发生的任何其他事件的更新


所以如果你同时拥有一个 ValueEventListener 和一个 ChildEventListener 在给定的位置, ValueEventListener.onDataChange()保证在 后被调用 code> onChildAdded()发生呼叫。您可以使用它来知道初始数据加载完成时间:

  ref.addListenerForSingleValueEvent(new ValueEventListener(){
public void onDataChange(DataSnapshot dataSnapshot){
System.out.println(我们完成加载初始+ dataSnapshot.getChildrenCount()+items);
}
public void onCancelled(FirebaseError firebaseError){}
});
ref.addChildEventListener(new ChildEventListener(){
public void onChildAdded(DataSnapshot dataSnapshot,String previousKey){
System.out.println(Add+ dataSnapshot.getKey()+ $ previous_ b $ b public void onChildMoved(DataSnapshot dataSnapshot,String s){
}
public void onCancelled(FirebaseError firebaseError){}
});

在我的测试运行中,结果是:

 将-K2WLjgH0es40OGWp6Ln添加到UI后null 
将-K2YyDkM4lUotI12OnOs添加到UI之后-K2WLjgH0es40OGWp6Ln
将-K2YyG4ScQMuRDoFogA9添加到-K2YyDkM4lUotI12OnOs
...
将-K4BPqs_cdj5SwARoluP添加到-K4A0zkyITWOrxI9-2On
之后添加-K4BaozoJDUsDP_X2sUu到UI -K4BPqs_cdj5SwARoluP
将-K4uCQDYR0k05Xqyj6jI添加到UI之后-K4BaozoJDUsDP_X2sUu
我们完成了加载初始的121个项目

所以你可以使用 onDataChanged()事件隐藏进度条。



但要注意一点:Firebase不仅仅是加载数据。它不断地将数据从服务器同步到所有连接的客户端。因此,没有真正的任何时候,数据是完全检索。


I'm using firebase for storing and retrieving data for my android application, in my activity i'm retrieve all data (example: list user data) from firebase using childEventListener.

My problem is:

I want to show a progress bar as long as the data is not completely retrieved from firebase using childEventListener. How to check if all data is completely retrieved? because i want to close the progress bar after the data is completely retrieved.

Any answer would be appreciated, thanks!

解决方案

There is a common way to detect when Firebase is done synchronizing the initial data on a given location. This approach makes use of one of the Firebase event guarantees:

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

So if you have both a ValueEventListener and a ChildEventListener on a given location, the ValueEventListener.onDataChange() is guaranteed to be called after all the onChildAdded() calls have happened. You can use this to know when the initial data loading is done:

ref.addListenerForSingleValueEvent(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println("We're done loading the initial "+dataSnapshot.getChildrenCount()+" items");
    }
    public void onCancelled(FirebaseError firebaseError) { }
});
ref.addChildEventListener(new ChildEventListener() {
    public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) {
        System.out.println("Add "+dataSnapshot.getKey()+" to UI after "+previousKey);
    }
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    }
    public void onChildRemoved(DataSnapshot dataSnapshot) {
    }
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    }
    public void onCancelled(FirebaseError firebaseError) { }
});

In my test run this results in:

Add -K2WLjgH0es40OGWp6Ln to UI after null
Add -K2YyDkM4lUotI12OnOs to UI after -K2WLjgH0es40OGWp6Ln
Add -K2YyG4ScQMuRDoFogA9 to UI after -K2YyDkM4lUotI12OnOs
...
Add -K4BPqs_cdj5SwARoluP to UI after -K4A0zkyITWOrxI9-2On
Add -K4BaozoJDUsDP_X2sUu to UI after -K4BPqs_cdj5SwARoluP
Add -K4uCQDYR0k05Xqyj6jI to UI after -K4BaozoJDUsDP_X2sUu
We're done loading the initial 121 items

So you could use the onDataChanged() event to hide the progress bar.

But one thing to keep in mind: Firebase doesn't just load data. It continuously synchronizes data from the server to all connected clients. As such, there is not really any moment where the data is completely retrieved.

这篇关于找出firebase上的子事件侦听器是否完全加载所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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