RealtimeDictionary.modelReplaced oldModel is null
I see the signature is: delegate void Normal.Realtime.Serialization.RealtimeDictionary<TValue>.ModelReplaced(Normal.Realtime.Serialization.RealtimeDictionary<TValue> dictionary, uint key, TValue oldModel, TValue newModel, bool remote)
However, when I try to access oldModel, it says the value is null. Is this expected?
12 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:
----------------------
Yes, it is possible and expected for oldModel to be null in the modelReplaced event of RealtimeDictionary. According to the documentation, the modelReplaced event is fired when a model is replaced locally or remotely. A model is considered replaced (as opposed to removed and added) if a new model is added that overwrites an existing model under the same key.
However, the documentation does not explicitly state under which circumstances oldModel will be null. In general, if there was no previous model under the given key (for example, if this is the first time a model is being set for that key), oldModel would be null. This can happen if the dictionary is being initialized or if the key did not previously exist.
If you are seeing oldModel as null, it likely means that there was no previous value for that key at the time of replacement. This behavior is consistent with how many dictionary-like data structures work in C# and Unity, where replacing a value for a non-existent key is effectively an "add" operation, and thus there is no old value to provide.
For more details on the event and its parameters, see the documentation: RealtimeDictionary<TValue>
----------------------
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.
Joining a room for a first time would always have a null old model, there was nothing present beforehand
OHHH DUHHH
š¤¦āāļø
replaced should always pass you the model being replaced
thereās a separate event for added / removed
would love a repro if youāre seeing this on dictionary.modelReplaced
Wouldn't the first join always have the oldmodel be null though? Since there was nothing to replace it with?
Weāre not talking about OnRealtimeModelReplaced, this is the RealtimeDictionary modelReplaced event that only fires when you insert a model under an existing key
it shouldnāt fire at all on first join
ohhhhh
my bad hahah
all good!
š

i clowned by not reading kyle's code
thatāll do it. glad it was an easy fix