Firebase Database overwrites previous value with setValue()

我的未来我决定 提交于 2020-01-06 12:47:10

问题


So I have a Database in Firebase.

This code is in onCreate():

DatabaseReference mDatabaseRefUser =FirebaseDatabase.getInstance().getReference("Users");

This code is in another method that gets called onClick:

mDatabaseRefUser.child("Chat").child(mAuth.getCurrentUser().getUid())
            .child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
            mAuth.getCurrentUser().getEmail()));

mDatabaseRefUser.child("Chat").child(userID)
            .child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
            userEmail));

What im trying to do is to add a new value without deleting the previous/existing value everytime the method gets called.

For the moment everytime I click and start the method, the previous/existing value gets removed and the new one gets added. But I want neither to be deleted. I want this the new value to be added aswell. I have tried with getRef and push() but my data still gets overwritten. What am I doing wrong here?

This is my JSON 1st time I call the method:

"Users" : {
"Chat" : {
  "Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
    "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
      "-LCvIA_9L1Y9rjkm96Aj" : {
        "Messages" : {
      "-LCvqDidnNqQ8ALZhDud" : {
        "messageText" : "Very good",
        "messageTime" : 1526791230552,
        "messageUser" : "mama@mama.com"
      }
    },
    "chatAgainstUserEmail" : "mama2@mama.com",
    "chatAgainstUserID" : "xfqAFlRpbUZRlZb76svN5FUtbU93",
    "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
    "userEmail" : "mama@mama.com",
    "userID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2"
  }
}

},
  "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
  "-LCvIA_9L1Y9rjkm96Aj" : {
    "Messages" : {
      "-LCvqDigI8ZwHDYuPhw9" : {
        "messageText" : "Very good",
        "messageTime" : 1526791230555,
        "messageUser" : "mama2@mama.com"
      }
    },
    "chatAgainstUserEmail" : "mama@mama.com",
    "chatAgainstUserID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2",
    "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
    "userEmail" : "mama2@mama.com",
    "userID" : "xfqAFlRpbUZRlZb76svN5FUtbU93"
      }
    }
  }
}

This is my JSON when I call it for the 2nd time

"Users" : {
"Chat" : {
  "Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
    "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
      "-LCvIA_9L1Y9rjkm96Aj" : {
        "Messages" : {
      "-LCvr0zoHOPxsKOVJzeu" : {
        "messageText" : "Very good 2nd call",
        "messageTime" : 1526791440547,
        "messageUser" : "mama@mama.com"
      }
        },
    "chatAgainstUserEmail" : "mama2@mama.com",
    "chatAgainstUserID" : "xfqAFlRpbUZRlZb76svN5FUtbU93",
    "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
    "userEmail" : "mama@mama.com",
    "userID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2"
  }
}
  },
  "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
  "-LCvIA_9L1Y9rjkm96Aj" : {
    "Messages" : {
      "-LCvr0zrh8wQ2WurUezs" : {
        "messageText" : "Very good 2nd call",
        "messageTime" : 1526791440550,
        "messageUser" : "mama2@mama.com"
      }
    },
    "chatAgainstUserEmail" : "mama@mama.com",
    "chatAgainstUserID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2",
    "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
    "userEmail" : "mama2@mama.com",
    "userID" : "xfqAFlRpbUZRlZb76svN5FUtbU93"
      }
    }
  }
}

I have also tried with getRef().push(), but that didn't work either. I have also tried without .getRef() and without .push(), didnt work either.


回答1:


To update a value instead of overriding it, you should use updateChildren() method.

You are overriding it because every node in a Firebase database is a Map and in the case of Map, it replaces the old value with the new one.




回答2:


To update data you should go for such kind of code instead of using child.setValue() as its name suggests it will directly replace old values with new values. But If you want to change a specific value then you should go for something as below. Give it a try mate!

HashMap<String, Object> params = new HashMap<>();
params.put("yourKey", your value);   
yourDatabaseChildReference.updateChildren(params);



回答3:


My only solution to this issue was to make a new Database,

I added this:

mDatabaseRefUser.child("Chatting").child(mAuth.getCurrentUser().getUid())
                .child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
                mAuth.getCurrentUser().getEmail()));

        mDatabaseRefUser.child("Chatting").child(userID)
                .child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
                userEmail));

and removed the old one



来源:https://stackoverflow.com/questions/50435475/firebase-database-overwrites-previous-value-with-setvalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!