When using Firebase 2.4 ref.updateChildren()
with HashMap, other than HashMap
(e.g. HashMap
)
As in the code snippet from the Official Firebase Blog to avoid this issue one has to re-write custom structure like this:
Map<String, String> newPost = new HashMap<String, String>();
newPost.put("title", "New Post");
newPost.put("content", "Here is my new post!");
And then put it instead of custom data model:
Map<String, Object> updatedUserData = new HashMap<String, Object>();
updatedUserData.put("users/posts/" + newPostKey, true);
updatedUserData.put("posts/" + newPostKey, newPost);
Using custom data model instead of newPost
causes IllegalStateException.
On your last question:
Is there a way to pass custom HashMap like HashMap to ref.updateChildren() ?
The Firebase SDK for Android/Java doesn't support directly passing POJOs (like your User
class) into updateChildren()
.
But what you can do is convert the POJO into a Map
with:
Map<String, Object> userMap = new ObjectMapper().convertValue(user, Map.class);
Then you can put the map of values into the values that you're passing into updateChildren()
:
updates.put("users/"+uid, userMap);
updates.put("lists/"+uid+"/mine, "value");
ref.updateChildren(updates);