I want to get a list of key-value pairs from my firebase database, remove one pair according to some conditions, then send back the list to my database. The number of pairs in the list can grow or shrink. I want to use a transaction because I want to be sure that the list has not been modified by somebody else. Here is my code:
mFirebaseDatabase.getReference().child("mychild").runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Log.d("bloup", mutableData.toString());
Log.d("bloup", String.valueOf(mutableData.getChildrenCount()));
Map <String,String> myDataBaseData = mutableData.getValue(HashMap.class);
Log.d("bloupp", "bloupp");
Log.d("bloupp", myDataBaseData.toString());
// loop all result
for (final MutableData postSnapshot : mutableData.getChildren()) {
if (mycondition) {
myDataBaseData.remove(postSnapshot.getKey().toString());
break;
}
}
mutableData.setValue(myDataBaseData);
return Transaction.success(mutableData);
}
Here is what I get in my logs:
bloupopp: MutableData { key = , value = {abc=123, def=456, ghi=789} }
bloupopp: 3
So I guess there is an issue with
Map <String,String> myDataBaseData = mutableData.getValue(HashMap.class);
But I don't see why. Any idea?