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.
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