Stanley Giovany
Stanley Giovany

Reputation: 583

Find out if childEventListener on Firebase has completed loading all data

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?

Upvotes: 47

Views: 37458

Answers (3)

Rizvan Khatri
Rizvan Khatri

Reputation: 1

Use

  totalChilds = 0; 
  ref..addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    totalChilds = dataSnapshot.getChildrenCount();
                    ref.addChildEventListener(new ChildEventListener() {
                           @Override
                           public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                           count++;
                           if(count >= totalChilds){
                                 progressDialog.dismiss();
            }
            @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

Upvotes: 0

osama
osama

Reputation: 1296

I have done it in quite simple way. I let an int count and then every time when it comes inside the function , i increment it and check it whether it is equals to the total no of childs . if it's equal then there you can stop the progress bar

    int count = 0;    
    ref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            count++;


            if(count >= dataSnapshot.getChildrenCount()){
                //stop progress bar here
            }
        }

Upvotes: 13

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

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.

Upvotes: 96

Related Questions