New in version 10.2.0.
A Realm set, like the C# HashSet<> , is an implementation of ICollection<> and IEnumerable<> . It supports values of any Realm type except collections. To define a set, use a getter-only ISet<TValue>
property, where TValue
is any of the supported types.
Deleting an object from the database will remove it from any sets in which it existed. Therefore, a set of objects will never contain null objects. However, sets of primitive types can contain null values. If you do not want to allow null values in a set, then either use non-nullable types in the set declaration (for example, use ISet<double>
instead of ISet<double?>
). If you are using the older schema type definition (your classes derive from the RealmObject
base class), or you do not have nullability enabled, you will need to use the [Required] attribute if the set contains nullable reference types, such as string
or byte[]
.
Local-only realms support collections of nullable (optional) values, but Sync
does not.
The following code shows examples of set types:
public partial class Inventory : IRealmObject{ public ISet<Plant> PlantSet { get; } public ISet<double> DoubleSet { get; } public ISet<int?> NullableIntsSet { get; } public ISet<string> RequiredStrings { get; }}
The following code shows how to create, write to, and read from Sets.
var inventory = new Inventory();inventory.PlantSet.Add(new Plant() { Name = "Prickly Pear" });inventory.DoubleSet.Add(123.45);realm.Write(() =>{ realm.Add<Inventory>(inventory);});var pricklyPear = inventory.PlantSet.AsRealmQueryable() .Where(p => p.Name == "Prickly Pear");var pricklyPearPlants = inventory.PlantSet .Filter("Name == 'Prickly Pear'");var moreThan100 = realm.All<Inventory>() .Filter("DoubleSet.@values > 100");
You can use the INotifyCollectionChanged.CollectionChanged event on a set to watch for changes to the set, and the INotifyPropertyChanged.PropertyChanged event to watch for changes to specific properties in the set.
In the following code example, we have a class with an ISet<string>
property named StringSet
. We set up event handlers for both the CollectionChanged
and PropertyChanged
events:
var stringSet = container.StringSet.AsRealmCollection();stringSet.CollectionChanged += (sender, e) =>{ Console.WriteLine($"Set {sender} changed: {e.Action}");};stringSet.PropertyChanged += (sender, e) =>{ Console.WriteLine($"Property changed on {sender}: {e.PropertyName}");};
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4