java - Android Firebase - Why Null Values in Map fields are ignored -
i discovered when saving pojo map field using firebase on android, if map contains nulls in value property of map, whole field ignored.
the workaround easy (non-null values result in map saving successfully), want understand why so?
model
public class game { private string owner; private map<string, string> characters; public game() { // empty constructor firebase } public string getowner() { return owner; } public void setowner(string owner) { this.owner = owner; } public map<string, string> getcharacters() { return characters; } public void setcharacters(map<string, string> characters) { this.characters = characters; } }
calling code
final game game = new game(); game.setowner("owner"); game.setcharacters(new hashmap<string, string>()); game.getcharacters().put("bugs bunny", null); game.getcharacters().put("batman", null); final firebase ref = new firebase("<firebaseurl>/test/" + system.currenttimemillis()); ref.setvalue(game);
resulting object in firebase
{ "1456421340781": { "owner": "owner" } }
they're not ignored. when give firebase null
value property/path, indicate want property or path deleted.
from documentation on firebase's javascript set()
method:
passing null new value equivalent calling
remove()
; data @ location or child location deleted.
so if set value with:
ref.child("keith").setvalue(47649);
then following delete it:
ref.child("keith").setvalue(null);
this behavior useful when use updatechildren()
, works equally when call setvalue()
.
Comments
Post a Comment