10.2.0 版本中的新增功能。
Overview
Realm字典是 IDictionary 的实施,具有类型为String 的键并支持除集合之外的任何Realm类型的值。要定义字典,请使用仅限 getter 的IDictionary<string, TValue> 属性,其中TValue 是任何受支持的类型。
字典类型
对象字典可以包含空对象。同样,基元类型的字典也可以包含空值。如果您不希望字典支持空值,则可以在字典声明中使用非空类型(例如,使用 IDictionary<string, double> 而不是 IDictionary<string, double?>)。如果您使用旧的模式类型定义(类派生自 RealmObject 基类),或者未启用可空性,则在字典包含 string 或 byte[] 可空引用类型时,请使用 [Required] 属性。
Realm 不允许在映射键中使用 . 或 $ 字符。您可以使用百分比编码和解码来存储包含这些不允许的任一字符的映射键。
重要
同步不支持可空值
仅限本地 Realm 支持可为 null(可选)值的集合,但Sync不支持。
以下代码显示了字典类型的示例:
public partial class Inventory : IRealmObject { [] [] public string Id { get; set; } // The key must be of type string; the value can be // of any Realm-supported type, including objects // that inherit from RealmObject or EmbeddedObject public IDictionary<string, Plant?> Plants { get; } public IDictionary<string, bool> BooleansDictionary { get; } // Nullable types are supported in local-only // Realms, but not with Sync public IDictionary<string, int?> NullableIntDictionary { get; } public IDictionary<string, string> RequiredStringsDictionary { get; } }
使用示例
以下代码显示如何使用字符串查询 (RQL) 或 LINQ 创建、写入和读取字典。
var storeInventory = new Inventory() { Id = ObjectId.GenerateNewId().ToString() }; storeInventory.Plants.Add("Petunia", new Plant()); storeInventory.NullableIntDictionary.Add("random things", 7); storeInventory.RequiredStringsDictionary.Add("foo", "bar"); var storeInventory2 = new Inventory() { Id = ObjectId.GenerateNewId().ToString() }; storeInventory2.RequiredStringsDictionary.Add("foo", "Bar"); realm.Write(() => { realm.Add(storeInventory); realm.Add(storeInventory2); }); // Find all Inventory items that have "Petunia" // as a key in their Plants dictionary. var petunias = realm.All<Inventory>() .Filter("Plants.@keys == 'Petunia'"); // Find all Inventory items that have at least one value in their // IntDictionary that is larger than 5 using RQL var matchesMoreThanFive = realm.All<Inventory>() .Filter("NullableIntDictionary.@values > 5"); // Find all Inventory items where the RequiredStringsDictionary has a key // "Foo", and the value of that key contains the phrase "bar" // (case insensitive) var matches = realm.All<Inventory>() .Filter("RequiredStringsDictionary['foo'] CONTAINS[c] 'bar'"); // matches.Count() == 2 // Query the Plants dictionary of an Inventory object // for a specific plant var myStoreInventory = realm .All<Inventory>().FirstOrDefault(); var petunia = myStoreInventory.Plants.AsRealmQueryable() .Where(p => p.Name == "Petunia");
关注变更
您可以使用字典上的 INotifyCollectionChanged.CollectionChanged 事件来监视集合的变更,并使用 INotifyPropertyChanged.PropertyChanged 事件来监视字典中特定属性的变更。
在以下代码示例中,我们有一个类,它有名为 IntDictionary 的 IDictionary<string, int> 属性。我们为 CollectionChanged 和 PropertyChanged 事件设置事件处理程序:
var dictionary = container.IntDictionary.AsRealmCollection(); dictionary.CollectionChanged += (sender, e) => { Console.WriteLine($"Collection {sender} changed: {e.Action}"); }; dictionary.PropertyChanged += (sender, e) => { Console.WriteLine($"Property changed on {sender}: {e.PropertyName}"); };