Tord Larsen
Tord Larsen

Reputation: 2836

How to start a ProgressBar if Firebase onComplete take longer then 3 sec

I want to make a call to Firebase and if it takes more then lets say 5 sec before the onComplete surface I want to start a ProgressBar

Like this something:

// start some Thread here that will start `Progressbar`if 5 sec   
// passes before`onComplete` surface  

    Ref.updateChildren(childUpdates, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError error, DatabaseReference ref) {
         // Turn of `Progressbar`           
        }
    }

How would I do that

Upvotes: 0

Views: 246

Answers (1)

Jonathan Aste
Jonathan Aste

Reputation: 1774

First create a runnable and a handler on your class:

Handler handler = new Handler();
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        //show progress bar
    }
};

then setup the handler to run after 5 seconds:

handler.postDelayed(runnable, 5000);

Then on your onComplete method remove the callback to avoid the execution if the method last less than 5 secs.

handler.removeCallbacks(runnable);

Upvotes: 1

Related Questions