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

查看:15
本文介绍了查看 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 何时完成同步给定位置的初始数据.这种方法利用了 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,则保证会调用 ValueEventListener.onDataChange()所有 onChildAdded() 调用都发生之后.您可以使用它来了解初始数据加载何时完成:

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天全站免登陆