I have this piece of code in my program that should allow a user to tap on a message and the score should increase by one:
super.collectionView(collectionVie
You have set up an observer that should observe any value changes in the "messages"-tree and all of its children nodes.
This means that whenever you update the score, you'll also receive information on this in your observer - and your code will run again. To fix this so your score only will be set once you have to change your observer to only fetch changes once:
rootRef.child("messages").observeSingleEventOfType(.Value, withBlock:
Read more here: https://firebase.google.com/docs/database/ios/retrieve-data
I would also recommend you to take a look at the structure your database-section: https://firebase.google.com/docs/database/ios/structure-data
Ivan's answer will solve the current problem you have. But loading all messages from the server to detect which one the user clicked sounds like a potentially big waste of bandwidth.
If you're showing a list of messages from Firebase to the user, I recommend keeping track of the key of each message. With that information, you won't have scan all messages, but can instead directly look up the message that was clicked and increase its score with a transaction:
rootRef.child("messages").child(postID).child("score").runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
// Set value and report transaction success
currentData.value = currentData.value + 1
return FIRTransactionResult.successWithValue(currentData)
}) { (error, committed, snapshot) in
if let error = error {
print(error.localizedDescription)
}
}
A transaction will also solve the potential race condition you now have: if two users click the same message at almost the same time, one might be overwriting the score-increment of the other.