-2

I would like to pause my android app for 1 second after I inserted something into the Firebase database. For that I use the following code insider a liistener:

        firebase_DB.child(id).setValue(currentOrder).addOnCompleteListener(new OnCompleteListener<Void>() {
         @Override
            public void onComplete(@NonNull Task<Void> task) {
             if (task.isSuccessful()) {
                 orderSuccesfullyWritten[0] =true;
                 orderCouldNotBeSendAfterMutipleAttemps[0] = false;
                 Log.e("dbTAG",  "Data successfully written.");
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                } else {

                 Log.e("dbTAG", task.getException().getMessage());
                }
            }
    });

The code is being executed (and I get the message that "Data successfully written" but the Threas.sleep(1000) does not have any effects. The app just directly continues with the next actions. Any idea why this is the case? I'll appreciate every comment.

VanessaF
  • 515
  • 11
  • 36
  • Could you explain what you mean with pausing the app? Without using technical terms, what should pause? What happens: there are at least 2 threads, the UI thread and the thread firebase is running on. The firebase probably does pause, the UI thread continues to run. `Thread.sleep(1000);` is the last call of this function in this state, so it runs, pauses and then stops. – Michiel Sep 26 '21 at 09:57
  • `Thread.sleep()` does not 'pause the app` in any correct implementation of Java. It pauses the *current thread.* – user207421 Sep 26 '21 at 10:01
  • i think you're better off explaining why you need to pause your app instead of asking how to do it, because stopping the app (the main thread) isn't a good idea – a_local_nobody Sep 26 '21 at 10:01
  • @a_local_nobody The 'app' does not consist merely of the main thread; and pausing the main thread does not pause any of the others, and therefore does not pause the app at all; and there is no evidence here that the thread he is pausing is the main thread. – user207421 Sep 26 '21 at 10:03
  • @a_local_nobody: Thanks for your answers. Basically I would like to pause the UI in case of not being able to submit the data to the firebase query. In that case the user should wait for one second and afterwards the app tries to write again to firebase. – VanessaF Sep 26 '21 at 12:52
  • Why is it not a good idea to pause the UI for some seconds? Basically if you want to write something to firebase, then the device should have good Internet connection. Due to the fact that I use WLan it can happen, that there is no good Internet connection after submitting a query to the Firebase database. In that case the user should wait for some seconds before the programm tries to write something again to the Firebase database – VanessaF Sep 26 '21 at 12:56

2 Answers2

1

You are putting thread.sleep once the firebase query has already completed.

Simply move Thread.sleep to after your firebase query

firebase_DB.child(id).setValue(currentOrder).addOnCompleteListener(do something);
Thread.sleep(1000);

A firebase query runs asynchronously, meaning it is running on a background thread. So with this code the main thread will sleep for a second while the firebase query is executing

But be reminded that using Thread.sleep() is bad practice

Emmanuel Conradie
  • 345
  • 1
  • 5
  • 20
1

Just some additional information why Thread.sleep() called on main thread is bad idea:

  1. You will block the main thread (UI thread) in which all of the UI stuff happens -> this means you screen will 'freeze' and this is bad UX (you can't put a loader even if you want to).
  2. If you block the main thread more than 5 seconds then you will get "app is not responding" screen, and OS will kill your app, and the user can lost data.

Basically I would like to pause the UI in case of not being able to submit the data to the firebase query.

I would suggest you to put a loading screen telling the user the data is uploading... or smth like that.

Notron
  • 76
  • 7