0

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?

Community
  • 1
  • 1
Alex9494
  • 1,588
  • 3
  • 12
  • 22
  • Possible duplicate of [How to save users score in firebase and retrieve it in real-time in Android studio](https://stackoverflow.com/questions/48307610/how-to-save-users-score-in-firebase-and-retrieve-it-in-real-time-in-android-stud) – Alex Mamo Mar 15 '18 at 17:44
  • What is the problem? – Frank van Puffelen Mar 15 '18 at 17:50
  • Hi Alex thank you for your comment. My question is about getting a list of key-value pairs with a transaction so I don't think it is a duplicate. I already use transactions, I know how to retrieve integers or other data from my database. – Alex9494 Mar 15 '18 at 18:51
  • Hi Franck! My problem is that my logs after Map myDataBaseData... are not printed, and nothing happen in my database. So it seems that my code in doTransaction is not called after Map myDataBaseData..., this is my issue . I guess there is a mistake in my myDataBaseData declaration. The onComplete function is called. – Alex9494 Mar 15 '18 at 18:52

1 Answers1

0

I've just found how to make it work, I changed

Map <String,String> myDataBaseData = mutableData.getValue(HashMap.class);

by

Map <String,String> myDataBaseData = (Map) mutableData.getValue;

I don't know why but it works, all my code in doTransaction is called now :D

This answer helped me.

Alex9494
  • 1,588
  • 3
  • 12
  • 22