0

I'm using Firebase in my Android Project. I have a ValueEventListener and it keeps on getting the old value even the node is already modified by other devices. It seems like it is disregarding the updates made by other devices.

Here's my code:

ValueEventListener listener =  destinationDatabaseReference.child(somestring)
                    .addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if(dataSnapshot.getValue()==null)
                            {
                            }
                            else
                            {
                                //here I keep on getting the old value
                                string value = (long) dataSnapshot.child("someString").getValue();
                            }
                            }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mead Umandal
  • 373
  • 1
  • 2
  • 15
  • The code you shared seems fine. Do you by any change have disk persistence enabled? (If you don't: good, leave it that way) Are you sure the device has a network connection? How did you determine that the updates from the other device(s) made it to the database? – Frank van Puffelen Jan 03 '18 at 14:23

3 Answers3

0

use the query like this:-

Query query = mDatabase.getReference().child("posts").orderByChild("date").limitToLast(1);
query.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    if(dataSnapshot!=null){
                        String notific = String.valueOf(dataSnapshot.getValue());
                        Log.d("key",dataSnapshot.getKey());
                        Log.d("title",String.valueOf(dataSnapshot.child("title").getValue()));
                        Log.d("content",String.valueOf(dataSnapshot.child("content").getValue()));
                    }
                }
neo73
  • 434
  • 5
  • 20
0

Thanks guys for all the help. I tried your suggestions but it did not work for me :( However, I found this link and I would like to share just in case someone needs it.

For this kind of issue, you have to use Transaction Operations. Please see related useful links below:

Updating firebase data in several devices by Transaction Operations

Similar Question with Working Answer

Mead Umandal
  • 373
  • 1
  • 2
  • 15
-1

Try to use a final reference like this:

URL url = new URL(yourURL);
final Firebase root = new Firebase(url.toString());
root.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) DataSnapshot dataSnapshot) {
                            if(dataSnapshot.getValue()==null)
                            {
                            }
                            else
                            {
                                //here I keep on getting the old value
                                string value = (long) dataSnapshot.child("someString").getValue();
                            }
}
Code Pope
  • 5,075
  • 8
  • 26
  • 68