@@ -93,9 +93,6 @@ notification handler on the collection or handle the `CollectionChanged
93
93
<https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.inotifycollectionchanged.collectionchanged?view=net-6.0>`__
94
94
event.
95
95
96
- Register a Collection Change Listener
97
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98
-
99
96
You can register a notification handler on a specific
100
97
collection within a realm. The collection can be of realm objects
101
98
(like ``realm.All<Person>()``) or a collection property on a
@@ -110,7 +107,10 @@ Realm emits an initial notification when a subscription is added. After the
110
107
initial notification, Realm delivers notifications asynchronously whenever a
111
108
write transaction adds, modifies, or removes objects in the collection.
112
109
113
- Specifically, the notification contains a :dotnet-sdk:`ChangeSet <reference/Realms.ChangeSet.html>`
110
+ Notification ChangeSets
111
+ ~~~~~~~~~~~~~~~~~~~~~~~
112
+
113
+ The notification contains a :dotnet-sdk:`ChangeSet <reference/Realms.ChangeSet.html>`
114
114
with 6 properties:
115
115
116
116
- ``DeletedIndices`` is an ``int[]`` that contains the indices of the objects that were
@@ -129,6 +129,20 @@ with 6 properties:
129
129
structs that contain the previous and new index of an object moved within the
130
130
collection.
131
131
132
+ .. important:: Order Matters
133
+
134
+ In collection notification handlers, always apply changes
135
+ in the following order:
136
+
137
+ 1. deletions
138
+ #. insertions
139
+ #. modifications
140
+
141
+ Handling insertions before deletions may result in unexpected behavior.
142
+
143
+ Get Notified of All Collection Changes
144
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145
+
132
146
To subscribe to collection notifications, call the
133
147
:dotnet-sdk:`SubscribeForNotifications <reference/Realms.CollectionExtensions.html#Realms_CollectionExtensions_SubscribeForNotifications__1_System_Collections_Generic_IDictionary_System_String___0__Realms_NotificationCallbackDelegate_System_Collections_Generic_KeyValuePair_System_String___0___>`
134
148
method. ``SubscribeForNotifications`` returns a subscription token which can be
@@ -139,16 +153,17 @@ The following code shows how to observe a collection for changes.
139
153
.. literalinclude:: /examples/generated/dotnet/WorkWithRealm.snippet.collection-notifications.cs
140
154
:language: csharp
141
155
142
- .. important:: Order Matters
143
-
144
- In collection notification handlers, always apply changes
145
- in the following order:
156
+ Limit Notifications
157
+ ~~~~~~~~~~~~~~~~~~~
146
158
147
- 1. deletions
148
- #. insertions
149
- #. modifications
150
-
151
- Handling insertions before deletions may result in unexpected behavior.
159
+ The SDK provides also provides a
160
+ :dotnet-sdk:`KeyPathsCollection <reference/Realms.KeyPathsCollection.html>`, which
161
+ provides a way to filter the fields that will trigger a notification. You pass
162
+ the ``KeyPathsCollection`` to the ``SubscribeForNotifications`` method.
163
+ The following code shows how to observe specific fields:
164
+
165
+ .. literalinclude:: /examples/generated/dotnet/WorkWithRealm.snippet.field-notifications.cs
166
+ :language: csharp
152
167
153
168
.. _dotnet-unregister-a-change-listener:
154
169
0 commit comments