Niv
Niv2d ago

is StringKeyDictionary<TValue> a property I can put in models?

It seems to want TValue to be a model of its own, how am I supposed to use it?
2 Replies
kapa.ai
kapa.ai2d ago
Hi @Niv. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you can use StringKeyDictionary<TValue> as a property in your RealtimeModel, but with some important requirements: - TValue must be a RealtimeModel (i.e., a class marked with the [RealtimeModel] attribute). StringKeyDictionary is designed to synchronize collections of models, not arbitrary data types. - You cannot use primitive types (like int, string, etc.) as TValue; it must be a model class that Normcore can serialize and synchronize. How to use it: 1. Define your model class for TValue, for example:
[RealtimeModel]
public partial class MyItemModel {
[RealtimeProperty(1, true)]
private string _name;
}
[RealtimeModel]
public partial class MyItemModel {
[RealtimeProperty(1, true)]
private string _name;
}
2. Then, in your parent model, declare the StringKeyDictionary property:
[RealtimeModel]
public partial class MyCollectionModel {
[RealtimeProperty(1, true)]
private StringKeyDictionary<MyItemModel> _items;
}
[RealtimeModel]
public partial class MyCollectionModel {
[RealtimeProperty(1, true)]
private StringKeyDictionary<MyItemModel> _items;
}
Usage notes: - StringKeyDictionary is transactional, meaning changes are only reflected after the server confirms them. - You interact with the dictionary using methods like Insert, Remove, and TryGetValue, and you can listen to events such as didInsertModelForKey and didRemoveModelForKey for updates. For more details, see the documentation on StringKeyDictionary and the collections overview.
It is not possible to use built-in C# collections like Dictionary or List in models; you must use Normcore's provided collection types, and the value type must be a model, not a primitive or arbitrary class.
Supported Primitives ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2d ago
<@269927864265211906> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?