Firebase value event listener not working

后端 未结 3 1089
梦谈多话
梦谈多话 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! (^_^)

    0 讨论(0)
  • 2021-01-18 09:28

    Two things come to mind from this question, one about Firebase listeners and the second about removing excess code.

    A fundamental thing about Firebase listeners is that they are asynchronous, meaning your code does not wait for the result before executing the next line. So look at the comments in this skeleton code:

    userRef.child("id").
    addSingleValueEventListener(new ValueEventListener() {
        @Override public void onDataChange (DataSnapshot dataSnapshot){
            // code here does not get executed straight away,
            // it gets executed whenever data is received back from the remote database
        }
    
        @Override public void onCancelled (DatabaseError databaseError){
    
        }
    });
    // if you have a line of code here immediately after adding the listener
    // the onDataChange code *won't have run*. You go straight to these lines
    // of code, the onDataChange method will run whenever it's ready.
    

    So this means that if you want to do something with the data you are getting in onDataChange you should put that code inside the onDataChange method (or some other method called from there or in some other way running that code after the data has been delivered back).

    Regarding the second part, a slightly more Firebasey way of checking for existence of an int and getting the value would be:

    @Override public void onDataChange (DataSnapshot dataSnapshot){
        if (dataSnapshot.exists()) {
            id = dataSnapshot.getValue(Integer.class);
        } else {
            id = 1;
        }
    }
    
    0 讨论(0)
  • 2021-01-18 09:34

    you need to set id = 1 into firebase database using setValue(), try below line of code it might help you

       userRef.child("id").
    
      addSingleValueEventListener(new ValueEventListener() {
    @Override public void onDataChange (DataSnapshot dataSnapshot){
        try {
            String value = dataSnapshot.getValue().toString();
            id = Integer.parseInt(value);
        } catch (NullPointerException e) {
             // if id=0 then set id=1 using setvalue()
            // this will set value of Id =1 in firebase database 
                 id=1;                 
            userRef.child("id").setValue(id);
            log.d(LOG_TAG, e.printStackTrace());
        }
    }
    
    @Override public void onCancelled (DatabaseError databaseError){
                id=1;
              userRef.child("id").setValue(id);
       }
    }); 
    
    0 讨论(0)
提交回复
热议问题