Ced
Ced3w ago

RealtimeDictionary: new issue with 2.13.2 ?

Hello, I used to clear my RealtimeDictionary this way in version 2.10.4: while (blocks.Count > 0) blocks.Remove(blocks.First().Key); Where blocks is generated model from: [RealtimeProperty(1, true, true)] private RealtimeDictionary<BlockDataRealtimeModel> _blocks; However in version 2.13.2, RealtimeDictionary.Count seems not updated after a Remove. Am I missing something ? Is there a cleaner way to clear a RealtimeDictionary ?
4 Replies
kapa.ai
kapa.ai3w ago
Hi @Ced. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Thank you for your question about RealtimeDictionary in NormalVR. It seems there might be a change in behavior between versions 2.10.4 and 2.13.2. Let's address your concerns based on the available information. Regarding the issue with RealtimeDictionary.Count not updating after a Remove operation in version 2.13.2, the knowledge sources don't provide specific information about this change. However, this could indeed be a new issue or a change in behavior in the newer version. As for clearing a RealtimeDictionary, there isn't a direct "Clear" method mentioned in the documentation. However, based on the RealtimeDictionary documentation, there are a couple of approaches you could try: 1. Use the Remove method in a loop, but instead of relying on Count, you could use the ContainsKey method:
while (blocks.ContainsKey(blocks.First().Key))
{
blocks.Remove(blocks.First().Key);
}
while (blocks.ContainsKey(blocks.First().Key))
{
blocks.Remove(blocks.First().Key);
}
2. Alternatively, you could create a temporary list of keys and then remove them:
var keys = blocks.Keys.ToList();
foreach (var key in keys)
{
blocks.Remove(key);
}
var keys = blocks.Keys.ToList();
foreach (var key in keys)
{
blocks.Remove(key);
}
These approaches should work regardless of whether Count is updating correctly or not. If you're still experiencing issues with these methods, it might be worth reporting this as a potential bug to the NormalVR team, especially if the behavior has changed between versions. For the most up-to-date and accurate information, I recommend checking the Normcore documentation or reaching out to their support channels directly. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@308688372774469632> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
NormalMark
NormalMark3w ago
Hey, thanks for reporting this bug. I'll investigate I tried to repro using this test script:
c#
using System.Linq;
using Normal.Realtime;
using Normal.Realtime.Serialization;
using UnityEngine;

[RealtimeModel]
public partial class BlockDataRealtimeModel
{
[RealtimeProperty(1, true)]
private int _id;
}

[RealtimeModel]
public partial class DictionaryTestModel
{
[RealtimeProperty(1, true, true)]
private RealtimeDictionary<BlockDataRealtimeModel> _blocks;
}

public class DictionaryTest : RealtimeComponent<DictionaryTestModel>
{
protected override void OnRealtimeModelReplaced(DictionaryTestModel previousModel, DictionaryTestModel currentModel)
{
if (currentModel != null)
{
currentModel.blocks[1] = new BlockDataRealtimeModel() { id = 1 };
currentModel.blocks[2] = new BlockDataRealtimeModel() { id = 2 };
currentModel.blocks[3] = new BlockDataRealtimeModel() { id = 3 };
currentModel.blocks.Add(4, new BlockDataRealtimeModel() { id = 4 });
currentModel.blocks[3] = null;
currentModel.blocks[2] = new BlockDataRealtimeModel() { id = 20 };
}
}

[ContextMenu("Test")]
private void Test()
{
while (model.blocks.Count > 0)
{
var removed = model.blocks.Remove(model.blocks.First().Key);
Debug.LogError($"Removed: {removed}");
}
}
}
c#
using System.Linq;
using Normal.Realtime;
using Normal.Realtime.Serialization;
using UnityEngine;

[RealtimeModel]
public partial class BlockDataRealtimeModel
{
[RealtimeProperty(1, true)]
private int _id;
}

[RealtimeModel]
public partial class DictionaryTestModel
{
[RealtimeProperty(1, true, true)]
private RealtimeDictionary<BlockDataRealtimeModel> _blocks;
}

public class DictionaryTest : RealtimeComponent<DictionaryTestModel>
{
protected override void OnRealtimeModelReplaced(DictionaryTestModel previousModel, DictionaryTestModel currentModel)
{
if (currentModel != null)
{
currentModel.blocks[1] = new BlockDataRealtimeModel() { id = 1 };
currentModel.blocks[2] = new BlockDataRealtimeModel() { id = 2 };
currentModel.blocks[3] = new BlockDataRealtimeModel() { id = 3 };
currentModel.blocks.Add(4, new BlockDataRealtimeModel() { id = 4 });
currentModel.blocks[3] = null;
currentModel.blocks[2] = new BlockDataRealtimeModel() { id = 20 };
}
}

[ContextMenu("Test")]
private void Test()
{
while (model.blocks.Count > 0)
{
var removed = model.blocks.Remove(model.blocks.First().Key);
Debug.LogError($"Removed: {removed}");
}
}
}
But no luck Could you send a repro project? My guess would be something outside the Remove loop is playing a role and it's missing from my test script
Ced
CedOP2w ago
Thx for your answer, unfortunately I'm late in my work ^^ I won't have the time to make a repro project until the end of the week

Did you find this page helpful?