0

I am downloading data from firebase realtime in my application. We download this data on the splashscreen. The splashscreen screen should not turn off before this data is downloaded. Couldn't find how to do this

This my code;

   mDatabase.child("/server/Time").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            long time = dataSnapshot.getValue(Long.class);
            MyApplication.getInstance().setmServer(new server(time));
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            hideProgressDialog();
        }
    });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MustapTR
  • 57
  • 6

1 Answers1

2

If you only want to hide the splash screen once the data has been read from Firebase, you should put the code to hide it inside onDataChange. So:

mDatabase.child("/server/Time").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        long time = dataSnapshot.getValue(Long.class);
        MyApplication.getInstance().setmServer(new server(time));
        hideProgressDialog();
        ... hide splash screen and perform other actions that depends on the data
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        hideProgressDialog();
    }
});

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for your suggestion. I want to close the splash screen after the download is complete and turn on the new activity. if (task.isSuccessful ()) {} Can't I use code like this using ValueEventListener? – MustapTR Apr 18 '21 at 01:17
  • In the value event listener you put the same code in `onDataChange` like I've shown in my answer. If you prefer working with tasks, you can also use the recently released `get` API: https://firebase.google.com/docs/database/android/read-and-write#read_data_once. Since this is a recent addition, you're likely to find much fewer examples for it, but the result will be exactly the same as putting the code in `onDataChange` as I've shown above. – Frank van Puffelen Apr 18 '21 at 01:32
  • If this process is completed, I print a log, but "1618709870981" sometimes takes 2 minutes to download this data, sometimes 10 seconds. Why do you think it works so slowly? https://prnt.sc/11lzbcb – MustapTR Apr 18 '21 at 01:38
  • I mean, something is happening that hasn't happened to me before and it takes a very long time to download a very small amount of data. What do you think about this? – MustapTR Apr 18 '21 at 01:39
  • That's impossible to say based on the information you shared so far, but also seems like a separate question from where to hide the splashscreen, which is what I answered here. – Frank van Puffelen Apr 18 '21 at 01:43