Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add description and new code example #3362

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ Add links to every SDK's pages where you got the SDK-specific information:

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

### Release Notes

<!--
- **Define Data Access Permissions**
- Data Access Role Examples: Update CRUD Permissions example screenshots and
copyable JSON
-->

### PR Author Checklist

Before requesting a review for your PR, please check these items:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/netflify-preview-links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
id: build_page_links
run: |
new_links=""
base_link='https://deploy-preview-${{ github.event.number }}--docs-realm.netlify.app'
base_link='https://deploy-preview-${{ github.event.number }}--device-sdk.netlify.app'
changed_files=${{ steps.changed-files.outputs.all_changed_files }}
files=$(echo $changed_files | tr "," "\n")
for file in $files; do
Expand Down
27 changes: 27 additions & 0 deletions examples/dotnet/Examples/WorkWithRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,33 @@ public void Notifications()
});
//:snippet-end:

NotificationCallbackDelegate<Person> notificationCallback
= new NotificationCallbackDelegate<Person>((sender,changes) => {});
//:snippet-start:field-notifications
var query = realm.All<Person>();
KeyPathsCollection kpc;

// Use one of these equivalent declarations to
// specify the fields you want to monitor for changes:

kpc = KeyPathsCollection.Of("Email", "Name");
kpc = new List<KeyPath> {"Email", "Name"};

// To get all notifications for top-level properties
// and 4 nested levels of properties, use the `Full`
// static value:

kpc = KeyPathsCollection.Full;

// To receive notifications for changes to the
// collection only and none of the properties,
// use the `Shallow` static value:

kpc = KeyPathsCollection.Shallow;

query.SubscribeForNotifications(notificationCallback, kpc);
//:snippet-end:

//:snippet-start:unsub-collection-notifications
// Watch for collection notifications.
// Call Dispose() when you are done observing the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var query = realm.All<Person>();
KeyPathsCollection kpc;

// Use one of these equivalent declarations to
// specify the fields you want to monitor for changes:

kpc = KeyPathsCollection.Of("Email", "Name");
kpc = new List<KeyPath> {"Email", "Name"};

// To get all notifications for top-level properties
// and 4 nested levels of properties, use the `Full`
// static value:

kpc = KeyPathsCollection.Full;

// To receive notifications for changes to the
// collection only and none of the properties,
// use the `Shallow` static value:

kpc = KeyPathsCollection.Shallow;

query.SubscribeForNotifications(notificationCallback, kpc);
41 changes: 28 additions & 13 deletions source/sdk/dotnet/react-to-changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ notification handler on the collection or handle the `CollectionChanged
<https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.inotifycollectionchanged.collectionchanged?view=net-6.0>`__
event.

Register a Collection Change Listener
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can register a notification handler on a specific
collection within a realm. The collection can be of realm objects
(like ``realm.All<Person>()``) or a collection property on a
Expand All @@ -107,7 +104,10 @@ Realm emits an initial notification when a subscription is added. After the
initial notification, Realm delivers notifications asynchronously whenever a
write transaction adds, modifies, or removes objects in the collection.

Specifically, the notification contains a :dotnet-sdk:`ChangeSet <reference/Realms.ChangeSet.html>`
Notification ChangeSets
~~~~~~~~~~~~~~~~~~~~~~~

The notification contains a :dotnet-sdk:`ChangeSet <reference/Realms.ChangeSet.html>`
with 6 properties:

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

.. important:: Order Matters

In collection notification handlers, always apply changes
in the following order:

1. deletions
#. insertions
#. modifications

Handling insertions before deletions may result in unexpected behavior.

Get Notified of All Collection Changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

.. important:: Order Matters

In collection notification handlers, always apply changes
in the following order:
Limit Notifications
~~~~~~~~~~~~~~~~~~~

1. deletions
#. insertions
#. modifications

Handling insertions before deletions may result in unexpected behavior.
The SDK provides also provides a
:dotnet-sdk:`KeyPathsCollection <reference/Realms.KeyPathsCollection.html>`, which
provides a way to filter the fields that will trigger a notification. You pass
the ``KeyPathsCollection`` to the ``SubscribeForNotifications`` method.
The following code shows how to observe specific fields:

.. literalinclude:: /examples/generated/dotnet/WorkWithRealm.snippet.field-notifications.cs
:language: csharp

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

Expand Down
Loading