Firebase value event listener not working

后端 未结 3 1105
梦谈多话
梦谈多话 2021-01-18 08:56

In my app, I have an id that is supposed to be pulled down from firebase real time database. If I pull it down and it sees there is not an id available, then it sets the id

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 09:16

    I had kinda the same problem of getting an object out of FireBase Realtime Database, and as @Lewis McGeary wrote, I Just made my program Wait until all the data gets retrived from the Database

    Here's my sample code:

    BarReference = FirebaseDatabase.getInstance().getReference("Users").child(myUID);
    ValueEventListener UserChangeListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            me = dataSnapshot.getValue(UserDataSet.class);
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        }
    };
    BarReference.addValueEventListener(UserChangeListener);
    while (me == null){
       Log.w(TAG, "Waiting For The Data to Download");
       progresBar.setVisibility(View.VISIBLE);
       return;
    }
    
    

    Hope that Helps! (^_^)

提交回复
热议问题