Android Firebase 2.4 IllegalStateException using new ref.updateChildren()

后端 未结 2 1314
清酒与你
清酒与你 2020-12-10 08:43

When using Firebase 2.4 ref.updateChildren() with HashMap, other than HashMap (e.g. HashMap)

相关标签:
2条回答
  • 2020-12-10 09:12

    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.

    0 讨论(0)
  • 2020-12-10 09:33

    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);
    
    0 讨论(0)
提交回复
热议问题