Firebase Android addListenerForSingleValueEvent sometimes not returning data

前端 未结 4 1709
灰色年华
灰色年华 2021-01-17 10:34

When my application starts, I check the current user\'s details in my Firebase database (I\'m storing it\'s uid for that).

I\'m attaching addListenerForSingleValueEv

4条回答
  •  鱼传尺愫
    2021-01-17 11:12

    Please try like this. If you can't solver by this, send share me source code for this part. ...

    DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
    mDatabase.child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
    
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){
                final HashMap dataMap = (HashMap) dataSnapshot.getValue();
                for (String key : dataMap.keySet()){
                    Object data = dataMap.get(key);
                    try{
                        HashMap moneyData = (HashMap) data;
                        Money money = new Money(("", "");
                    }catch (ClassCastException cce){
                        try{
                            name = String.valueOf(dataMap.get("username"));
                            email = String.valueOf(dataMap.get("useremail"));
                        }catch (ClassCastException cce2){
                        }
                    }
                }                            
            // do something
            }else {
            // when empty data
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });
    

提交回复
热议问题