1

I'm having a problem with valueEvenListener on Firebase. Consider the situation:

Firebase ref = new Firebase(Constant.BASEURL).child("myLists");
ref.addValueEventListener(myValueEventListener);

Then I try do update one of the lists value using updateChildren().

HashMap<String, Object> newEditionTimeHM = new HasMap<String, Object>
newEditionTimeHM.put("editionTimeStamp", ServerValue.TIMESTAMP);

ref.child(pushKey).updateChildren(newEditionTimeHM);

It updates but when my listener catches the update, it crashes reporting "failed to bounce to type" error.

On my second try, I create a new shoppinList object with the new values I want and push it using setValue().

newEditionTimeHM.put("editionTimeStamp", ServerValue.TIMESTAMP);
ShoppingList shoppingList = new ShoppingList("Owner unknown", newEditionTimeHM);

ref.child(pushKey).setValue(shoppingList);

When my listeners catches it, no problem is reported.

What is the difference here? Why do I need to create an entirely new object to satisfy the listener? Isn't it more efficient to use updateChildren()? Searched this page for the answer but did not find it.

Andre Haueisen
  • 468
  • 2
  • 8
  • 20
  • If it "fails to bounce to type" then the resulting data (onDataChange) in one of the cases is probably different than in other. Check on debug what's the difference and draw conclusions. – Wukash Mar 25 '16 at 16:28
  • @Wukash I checked. These two methods should do the exact same thing. Proof of that is that when I check my firebase end result I get the exact same thing using the first method or the second. The difference is on how the listener reacts. – Andre Haueisen Mar 25 '16 at 17:02
  • Yes I understand that values on server are the same in the end result. What I said is that you should check what's the difference in the listener method and act accordingly. I mean we're not gonna be able to change the behaviour of firebase library so that's your only option... – Wukash Mar 25 '16 at 17:06

2 Answers2

2

ServerValue.TIMESTAMP has the type is Map<String, String>, so I guess your data structure in Firebase is related to this if your pushKey is "<your Firebase URL>/myLists":

"myLists": {
    "editionTimeStamp": {
        "timeStampKey": "timeStampValue"
    }
}

So, if you want to get TIMESTAMP value, i suggest your first try must be:

ref.child("<your Firebase URL>/myLists/editionTimeStamp/timeStampKey");
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        String timeStampValue = (String)snapshot.getValue();
    }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }
});

If it's not your solution, here is a good explanation about this error: Why do I get "Failed to bounce to type" when I turn JSON from Firebase into Java objects?
Hope it help :D

Mahesh Babariya
  • 4,560
  • 6
  • 39
  • 54
lalala
  • 36
  • 4
1

Ok. Found the problem. I realised reading this page and the answer above that I misunderstood how firebase listeners work. There is a diference on reacting to setValue() and updateChildren(). Make sure to check what values you require after the update and use ChildEventListener or ValueEventListener accordingly. Life is good again! Cheers!

Andre Haueisen
  • 468
  • 2
  • 8
  • 20