查明Firebase上的childEventListener是否已完成加载所有数据 [英] Find out if childEventListener on Firebase has completed loading all data

查看:72
本文介绍了查明Firebase上的childEventListener是否已完成加载所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase实时数据库为我的Android应用程序存储和检索数据.在我的活动中,我使用childEventListener从Firebase检索所有数据(例如:用户数据列表).

I'm using Firebase Realtime Database for storing and retrieving data for my Android application. In my activity I retrieve all data (example: list of user data) from Firebase using a childEventListener.

只要数据没有从数据库中完全检索出来,我想显示一个进度条.如何检查是否已完全检索所有数据,以便在加载数据后关闭进度栏?

I want to show a progress bar as long as the data is not completely retrieved from the database. How do I check if all data is completely retrieved so that I can close the progress bar after the data is loaded?

推荐答案

有一种通用方法可以检测Firebase在给定位置上完成 initial 数据同步的时间.这种方法利用了 Firebase事件保证之一:

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.

因此,如果在给定位置上同时具有ValueEventListenerChildEventListener,则在所有onChildAdded()调用都发生之后,保证ValueEventListener.onDataChange()被称为.您可以使用它来了解何时完成初始数据加载:

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

因此您可以使用onDataChanged()事件隐藏进度栏.

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

但要记住一件事:Firebase不仅加载数据.它将服务器中的数据持续同步到所有连接的客户端.因此,实际上根本没有任何时间完全检索数据.

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上的childEventListener是否已完成加载所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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