Java Android firebase – why ignore null values in map fields
•
Android
When I use firebase on Android to save POJOs using the map field, I find that if the map contains null in the value attribute of the map, the whole field will be ignored
The solution is simple (non null values will successfully save the map), but I want to know why?
Model
public class Game {
private String owner;
private Map<String, String> characters;
public Game() {
// empty constructor for 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;
}
}
Call 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);
Result object in firebase
{
"1456421340781": {
"owner": "owner"
}
}
resolvent:
They are not actually ignored. When you provide firebase with a null value for the property / path, it means that you want to delete the property or path
Start with documentation on firebase's JavaScript set() method:
Therefore, if you set the value using the following method:
ref.child("keith").setValue(47649);
Then delete it as follows:
ref.child("keith").setValue(null);
This behavior is most useful when you use updatechildren(), but it is also valid when you call setvalue()
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码