I have a messaging app with with models looking like this
public class Message {
String message;
List<String> reads;
... more logics
}
On firebase it looks like this
... [
"myGeneratedMessageId" : {
"from":"userId1",
"reads":[
"userId2",
"userId3"
]
}, ...
]
When a user sees it the user's id should be added to the array. Will I have to worry about overwriting values in that array if multiple users are seeing it simultaneously? And what do I do to make that not happening?
For clarification,
User 1 sees the message -> sends the user id to that array (I guess by adding it to the local array and then updating that child on the message on Firebase)
User 2 sees the message before User 1 updates it, updating it too - with adding only his id to the array and updating the child on Firebase
Edit: Updated usage (potential problem still exists)
I made reads
into a hashmap with id : boolean
...
"message": "Hello world!",
"from": "userId1",
"reads":[
"userId2": true,
"userId3": true
], ...
Inside model markAsRead()
if(reads == null) {
reads = new HashMap<String, Boolean>(); <-- could cause problem
}
if(reads.get(userId) == null) {
reads.put(userId, true);
}
.. save logics
A user could still create the hashmap if there is none, making the other users' new hashmap rewrite it.