If multiple clients run the following code to clear a RealtimeDictionary, what happens?
foreach (var kvp in model.playerPoints)
{
model.playerPoints.Remove(kvp.Key);
}
Where
[RealtimeProperty(1, true, true)]
private RealtimeDictionary<PlayerPointsModel> _playerPoints;
2 Replies
Hi @khoi. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
If multiple clients run the code
on a
RealtimeDictionary
, the following will occur:
- All updates are applied instantly and are rolled back if rejected by the server (due to ownership). This means that when a client removes a key, the change is reflected locally and sent to the server. If another client removes the same key at the same time, both clients will attempt to remove the same entry.
- No datastore conflicts are introduced by simultaneous modifications. The RealtimeDictionary
is specifically designed to allow multiple clients to modify it at the same time without causing conflicts in the datastore. The server will process each removal, and if a key is already removed by another client, the operation will simply have no effect for subsequent removals of the same key. This is handled gracefully and does not result in errors or inconsistent state across clients.
This behavior is described in the documentation:
RealtimeDictionary is a special model type that represents a dictionary of models. This collection can be modified by multiple clients simultaneously without introducing datastore conflicts. All updates are applied instantly and are rolled back if rejected by the server (due to ownership). Adding or removing items sends the minimal amount of information to the server in order to perform the update on all clients. The whole collection is not sent every time.RealtimeDictionary<TValue> So, in summary: If multiple clients try to remove the same keys at the same time, the removals are processed without conflict, and the dictionary will end up empty as intended. There is no error or desync, and the system is designed to handle this scenario. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@412870643533479936> Kapa.ai is still learning and improving, please let me know how I did by reacting below.