wait until firebase retrieves data

后端 未结 9 1132
不思量自难忘°
不思量自难忘° 2020-11-29 08:33

I want to build a method that returns a child value in FireBase. I tried to do something like this:

public String getMessage(){

           


        
9条回答
  •  鱼传尺愫
    2020-11-29 08:51

    Firebase usually takes about 1-2 seconds to load the data.

    So keeping that in mind you can add a new thread using Handler, to make the system wait for say, 2 Seconds and then use the value retrieved from the Database.

    Till then you can display a Loading Dialog box to the user which can be closed once the data is retrieved.

     Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    {
                        loadingDialog.dismiss();//dismiss the dialog box once data is retreived
                        Toast.makeText(Act_NewHome.this, "Not enough players online right now!", Toast.LENGTH_SHORT).show();
    tvUserName.setText(u_name);
                    }
                }
            }, 2000);// 2000 milliseconds = 2seconds
    

提交回复
热议问题