In this question answer, @Frank van Puffelen explains how Realtime Database increment atomic operations ServerValue.increment(1)
work:
Client Server Client
+ + +
increment(1) | | |
| | | increment(1)
| (increment, 1) | |
+--------->---------+ (increment, 1) |
| |---------<---------+
+---------<---------+ |
| (ack, 2) |--------->---------+
| | (ack, 3) |
| | |
v v v
What if, after sending "(increment, 1)", the client somehow loses connection and does not receive "(ack, 2)"?
In my tests, it simply retries and sometimes it causes duplicated incrementation. It happens sometimes under poor connection situations. :(
Is it an expected behaviour?
To reproduce the tests, simply execute the sample below while you repeatedly turn on and off your internet connection (WiFi or 4G), to emulate a poor connection situation:
val updates: MutableMap<String, Any> = HashMap()
updates["posts/$key/starCount"] = ServerValue.increment(1)
updates["user-posts/$uid/$key/starCount"] = ServerValue.increment(1)
database.updateChildren(updates)