Skip to content

Commit 24df3e6

Browse files
authored
Add description and new code example (#3362)
## Pull Request Info - SDK Docs Consolidation Jira ticket: [DOCSP-34405](https://jira.mongodb.org/browse/DOCSP-34405) ### Staging Links <!-- start insert-links --> <li><a href=https://deploy-preview-3362--device-sdk.netlify.app/sdk/dotnet/react-to-changes/#watch-for-collection-changes>sdk/dotnet/react-to-changes</a></li> <!-- end insert-links --> ### Release Notes - **.NET SDK** - Add KeyPathsCollection information & code snippet.
1 parent f8544c3 commit 24df3e6

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed

.github/pull_request_template.md

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ Add links to every SDK's pages where you got the SDK-specific information:
1313

1414
- [PAGE_NAME](https://www.mongodb.com/docs/atlas/device-sdks/LIVE-DOCS-LINK)
1515

16+
### Release Notes
17+
18+
<!--
19+
- **Define Data Access Permissions**
20+
- Data Access Role Examples: Update CRUD Permissions example screenshots and
21+
copyable JSON
22+
-->
23+
1624
### PR Author Checklist
1725

1826
Before requesting a review for your PR, please check these items:

.github/workflows/netflify-preview-links.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
id: build_page_links
2424
run: |
2525
new_links=""
26-
base_link='https://deploy-preview-${{ github.event.number }}--docs-realm.netlify.app'
26+
base_link='https://deploy-preview-${{ github.event.number }}--device-sdk.netlify.app'
2727
changed_files=${{ steps.changed-files.outputs.all_changed_files }}
2828
files=$(echo $changed_files | tr "," "\n")
2929
for file in $files; do

examples/dotnet/Examples/WorkWithRealm.cs

+27
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,33 @@ public void Notifications()
205205
});
206206
//:snippet-end:
207207

208+
NotificationCallbackDelegate<Person> notificationCallback
209+
= new NotificationCallbackDelegate<Person>((sender,changes) => {});
210+
//:snippet-start:field-notifications
211+
var query = realm.All<Person>();
212+
KeyPathsCollection kpc;
213+
214+
// Use one of these equivalent declarations to
215+
// specify the fields you want to monitor for changes:
216+
217+
kpc = KeyPathsCollection.Of("Email", "Name");
218+
kpc = new List<KeyPath> {"Email", "Name"};
219+
220+
// To get all notifications for top-level properties
221+
// and 4 nested levels of properties, use the `Full`
222+
// static value:
223+
224+
kpc = KeyPathsCollection.Full;
225+
226+
// To receive notifications for changes to the
227+
// collection only and none of the properties,
228+
// use the `Shallow` static value:
229+
230+
kpc = KeyPathsCollection.Shallow;
231+
232+
query.SubscribeForNotifications(notificationCallback, kpc);
233+
//:snippet-end:
234+
208235
//:snippet-start:unsub-collection-notifications
209236
// Watch for collection notifications.
210237
// Call Dispose() when you are done observing the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var query = realm.All<Person>();
2+
KeyPathsCollection kpc;
3+
4+
// Use one of these equivalent declarations to
5+
// specify the fields you want to monitor for changes:
6+
7+
kpc = KeyPathsCollection.Of("Email", "Name");
8+
kpc = new List<KeyPath> {"Email", "Name"};
9+
10+
// To get all notifications for top-level properties
11+
// and 4 nested levels of properties, use the `Full`
12+
// static value:
13+
14+
kpc = KeyPathsCollection.Full;
15+
16+
// To receive notifications for changes to the
17+
// collection only and none of the properties,
18+
// use the `Shallow` static value:
19+
20+
kpc = KeyPathsCollection.Shallow;
21+
22+
query.SubscribeForNotifications(notificationCallback, kpc);

source/sdk/dotnet/react-to-changes.txt

+28-13
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ notification handler on the collection or handle the `CollectionChanged
9393
<https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.inotifycollectionchanged.collectionchanged?view=net-6.0>`__
9494
event.
9595

96-
Register a Collection Change Listener
97-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98-
9996
You can register a notification handler on a specific
10097
collection within a realm. The collection can be of realm objects
10198
(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
110107
initial notification, Realm delivers notifications asynchronously whenever a
111108
write transaction adds, modifies, or removes objects in the collection.
112109

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>`
114114
with 6 properties:
115115

116116
- ``DeletedIndices`` is an ``int[]`` that contains the indices of the objects that were
@@ -129,6 +129,20 @@ with 6 properties:
129129
structs that contain the previous and new index of an object moved within the
130130
collection.
131131

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+
132146
To subscribe to collection notifications, call the
133147
: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___>`
134148
method. ``SubscribeForNotifications`` returns a subscription token which can be
@@ -139,16 +153,17 @@ The following code shows how to observe a collection for changes.
139153
.. literalinclude:: /examples/generated/dotnet/WorkWithRealm.snippet.collection-notifications.cs
140154
:language: csharp
141155

142-
.. important:: Order Matters
143-
144-
In collection notification handlers, always apply changes
145-
in the following order:
156+
Limit Notifications
157+
~~~~~~~~~~~~~~~~~~~
146158

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
152167

153168
.. _dotnet-unregister-a-change-listener:
154169

0 commit comments

Comments
 (0)