1

I try to create simple app lecturer-student. On login I receive boolean data from Firebase to check if user is student or lecturer. Then I go to "home view". The problem is that updating view is faster then receiving data and always first login after opening app is incorrect. It shows default value from SharedPrefference (after getting boolean i'm sharing it to home activity). Like in code below:

mDatabase.child("Users").child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            User data = dataSnapshot.getValue(User.class);
            lecturer = data.getLecturer();
            editor.putBoolean("Lecturer", lecturer).commit();
        }
}
updateUI();

Then in home activity I got:

boolean lecturer = pref.getBoolean("Lecturer", false);

if(lecturer == false) {
    setFragment(studentHome);
} else {
    setFragment(lecturerHome);
}    

Is there any simple solution to my problem? I would like to create some kind of promise to first receive data and then updateUI()

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Any code that needs access to the data from the database needs to be *inside* the `onDataChange` method, or be called from there. See https://stackoverflow.com/a/33204705, https://stackoverflow.com/a/51228910, https://stackoverflow.com/a/38460676, https://stackoverflow.com/a/46387410, https://stackoverflow.com/a/42141541, and https://stackoverflow.com/a/50435519. – Frank van Puffelen Feb 18 '19 at 21:33
  • I've just wanted to write that yeah it works, when it is inside, but I can't call function with firebase data retrieve. It must be every time the whole code. Looks awful but works for now and I don't have too much time for project. Thanks :) – Wojciech Jakubek Feb 18 '19 at 21:35
  • Check the last link I provided and the question I closed yours against, as it shows how to define your own callback interface. – Frank van Puffelen Feb 18 '19 at 21:36

1 Answers1

-1

I had a same problem because Firebase event listner works in seperate thread so i think your updateUI() in main thread so it executes after mDatabase eventlistner but the event listner works in seperate thread so simply put updateUI method inside eventListner in my understanding