Skip to content

(DOCSP-39538): Consolidate Manage Sync Sessions page #3285

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

Merged
merged 5 commits into from
Jun 25, 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
1 change: 1 addition & 0 deletions source/includes/api-details/cpp/api-not-supported.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The C++ SDK does not currently support this API.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
**Check Connection State**

To check the connection state, you can read the
:cpp-sdk:`sync session instance's <structrealm_1_1internal_1_1bridge_1_1sync__session.html>`
``connection_state()`` property directly.

.. literalinclude:: /examples/generated/cpp/sync-session.snippet.connection-state.cpp
:language: cpp

The network connection state is distinct from the Device Sync connection state
that you can check with the ``state()`` method. For more information about
sync connection state, refer to the Check the Sync State documentation on
this page.

**Observe Connection State**

You can also observe the connection state with the
:cpp-sdk:`observe_connection_change() <structrealm_1_1internal_1_1bridge_1_1sync__session.html#a38096e71024b3fd252d3356af5eee113>`
function. This function registers a callback that the SDK invokes when the
underlying sync session changes its connection state.

.. literalinclude:: /examples/generated/cpp/sync-session.snippet.observe-connection-change.cpp
:language: cpp

If you register a connection change listener, you can unregister it when
you're done listening for changes. Call the sync session instance's
:cpp-sdk:`unregister_connection_change_observer() <structrealm_1_1internal_1_1bridge_1_1sync__session.html#a86b911bc662f02f607c5e9513d80c0c7>`
method to unregister an observation token.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
You can use the :cpp-sdk:`sync_session
<structrealm_1_1internal_1_1bridge_1_1sync__session.html>`'s public
member function ``state()`` to check whether the sync session is active.
This returns an enum whose value reflects possible Device Sync states.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
You can use the member function :cpp-sdk:`get_sync_session()
<structrealm_1_1db.html#a79c5e6e92896703a54693be35720ae12>` to get a
:cpp-sdk:`sync_session
<structrealm_1_1internal_1_1bridge_1_1sync__session.html>`
object for any synced database. The SDK returns this object as an optional.
It is a lightweight handle that you can pass around by value.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You can choose to manually trigger a reconnect attempt with a sync session's
:cpp-sdk:`reconnect() <structrealm_1_1internal_1_1bridge_1_1sync__session.html#af5557f2a2ae8869e6a129afc264e8695>`
method.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
To pause a sync session, call the sync session's
:cpp-sdk:`pause() <structrealm_1_1internal_1_1bridge_1_1sync__session.html#ab187b267f0391c8262a0edcff32fcc29>`
method.

.. literalinclude:: /examples/generated/cpp/sync-session.snippet.pause.cpp
:language: cpp

To resume a sync session, call the sync session's
:cpp-sdk:`resume() <structrealm_1_1internal_1_1bridge_1_1sync__session.html#a253ef9e08d9f4cf7c42edfd6b6f6df80>`
method.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
You can use the
:cpp-sdk:`sync_session <structrealm_1_1internal_1_1bridge_1_1sync__session.html>`'s
``wait_for_upload_completion()`` and ``wait_for_download_completion()``
methods to wait for changes to upload to or download from Atlas. Both of these
methods can optionally take a callback to execute when upload or download
is complete.

To wait for all changes to upload to Atlas from your synced database,
use the member function ``wait_for_upload_completion()``.

.. literalinclude:: /examples/generated/cpp/sync-session.snippet.wait-for-upload.cpp
:language: cpp

To wait for all changes from Atlas
to download to your synced database, use the member function
``wait_for_download_completion()``. Refresh the database after downloading
any changes to be sure it reflects the most recent data.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
To check the connection state, you can read the
:dotnet-sdk:`Session instance's ConnectionState
<reference/Realms.Sync.Session.html#Realms_Sync_Session_ConnectionState>`
property. Its value is a :dotnet-sdk:`ConnectionState enum
<reference/Realms.Sync.ConnectionState.html>` that reflects the current state
of the sync session's connection.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
To check the Sync state, you can read the
:dotnet-sdk:`Session instance's State
<reference/Realms.Sync.Session.html#Realms_Sync_Session_State>`
property. Its value is a :dotnet-sdk:`ConnectionState enum
<reference/Realms.Sync.SessionState.html>` that reflects the current state
of the Sync session's communication with the server.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
To monitor Sync progress, get the Sync session from the
:dotnet-sdk:`Realms.Sync.SyncSession
<reference/Realms.Realm.html#Realms_Realm_SyncSession>` property, then add a
progress notification by calling the
:dotnet-sdk:`session.GetProgressObservable() <reference/Realms.Sync.Session.html#Realms_Sync_Session_GetProgressObservable_Realms_Sync_ProgressDirection_Realms_Sync_ProgressMode_>`
method.

The ``session.GetProgressObservable`` method takes in the following two parameters:

- A :dotnet-sdk:`ProgressDirection <reference/Realms.Sync.ProgressDirection.html>`
parameter that can be set to ``Upload`` or ``Download``.

- A :dotnet-sdk:`ProgressMode <reference/Realms.Sync.ProgressMode.html>` parameter
that can be set to ``ReportIndefinitely``
for the notifications to continue until the callback is unregistered, or
``ForCurrentlyOutstandingWork`` for the notifications to continue until only
the currently transferable bytes are synced.

When you `Subscribe <https://docs.microsoft.com/en-us/dotnet/api/system.iobservable-1.subscribe?view=net-6.0#system-iobservable-1-subscribe(system-iobserver((-0)))>`_
to the notifications, you receive a
:dotnet-sdk:`SyncProgress <reference/Realms.Sync.SyncProgress.html>`
object that provides a percentage estimate of the current progress, expressed
as a double between 0.0 and 1.0.

Once you no longer wish to receive notifications, unregister the token with
``token.Dispose()``.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You can access the Sync
:dotnet-sdk:`Session <reference/Realms.Sync.Session.html>` through a synced
:dotnet-sdk:`Realm <reference/Realms.Sync.html>`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
To pause synchronization for a specific session, you can call the
:dotnet-sdk:`Stop() <reference/Realms.Sync.Session.html#Realms_Sync_Session_Stop>`
method on the session.

When you then call the
:dotnet-sdk:`Start() <reference/Realms.Sync.Session.html#Realms_Sync_Session_Start>`
method on the paused session, the Sync session resumes.

The following code block demonstrates calling the ``Stop()`` and ``Start()``
methods:
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
To asynchronously wait for your changes to finish uploading, get the Sync
session from the :dotnet-sdk:`Realms.Sync.SyncSession
<reference/Realms.Realm.html#Realms_Realm_SyncSession>`
property, and then call the :dotnet-sdk:`session.WaitForUploadAsync()
<reference/Realms.Sync.Session.html#Realms_Sync_Session_WaitForDownloadAsync_System_Nullable_System_Threading_CancellationToken__>`
method.

To wait for changes to finish downloading, call the
:dotnet-sdk:`session.WaitForDownloadAsync()
<reference/Realms.Sync.Session.html#Realms_Sync_Session_WaitForUploadAsync_System_Nullable_System_Threading_CancellationToken__>`
method.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
You can get the state of the current network connection with
:flutter-sdk:`Session.connectionState <realm/Session/connectionState.html>`.
This returns a :flutter-sdk:`ConnectionState <realm/ConnectionState.html>` enum
that contains the network connection's state, which is one of:

- ``connected``
- ``disconnected``
- ``connecting``

.. literalinclude:: /examples/generated/flutter/manage_sync_session_test.snippet.get-network-connection.dart
:language: dart

Monitor the state of the network connection with
:flutter-sdk:`Session.connectionStateChanges <realm/Session/connectionStateChanges.html>`.
This property returns a Stream of :flutter-sdk:`ConnectionStateChange <realm/ConnectionStateChange-class.html>`
objects that updates when the network connection changes.
You can access the current and previous ``ConnectionState`` from ``ConnectionStateChange``.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
You can get the state of the current Sync session state with
:flutter-sdk:`Session.state <realm/Session/state.html>`.
This returns a :flutter-sdk:`SessionState <realm/SessionState.html>` enum
that contains the Sync session state, which is one of:

- ``active``
- ``inactive``
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.. versionchanged:: 2.0.0
``transferredBytes`` and ``transferrableBytes`` deprecated in favor of ``progressEstimate``

To monitor Sync progress, call :flutter-sdk:`SyncSession.getProgressStream()
<realm/Session/getProgressStream.html>`. This method returns a Stream of
:flutter-sdk:`SyncProgress <realm/SyncProgress-class.html>` objects that provide
a ``progressEstimate`` for the current upload or download.

The provided ``progressEstimate`` is a double whose value
ranges from ``0.0`` to ``1.0``. At ``1.0``, the progress stream is complete.

``SyncSession.getProgressStream()`` takes two arguments:

- A :flutter-sdk:`ProgressDirection <realm/ProgressDirection.html>`
enum that can be set to ``upload`` or ``download``. Specifies whether the
progress stream monitors upload or download progress.

- A :flutter-sdk:`ProgressMode <realm/ProgressMode.html>` enum
that can be set to one of the following:

- ``reportIndefinitely``: Sets notifications to continue until the callback is
unregistered.
- ``forCurrentlyOutstandingWork``: Sets notifications to continue until the
``progressEstimate`` reaches ``1.0``.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You can access the :flutter-sdk:`Session <realm/Session-class.html>` of any
synced database through the :flutter-sdk:`Realm.syncSession
<realm/Realm/syncSession.html>` property.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You can choose to manually trigger a reconnect attempt with the
:flutter-sdk:`App.reconnect() <realm/App/reconnect.html>` instead of waiting for
the duration of the incremental backoff.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
To pause syncing for a session, call :flutter-sdk:`Session.pause() <realm/Session/pause.html>`.
The database does not sync changes with Atlas while the session is paused.

To resume syncing a changes, call :flutter-sdk:`Session.resume() <realm/Session/resume.html>`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
To wait for the session to finish uploading all pending changes, call
:flutter-sdk:`Session.waitForUpload() <realm/Session/waitForUpload.html>`.

To wait for the session to finish downloading all pending changes, call
:flutter-sdk:`Session.waitForDownload() <realm/Session/waitForDownload.html>`.

Both of these sessions can take an optional :flutter-sdk:`CancellationToken
<realm/CancellationToken-class.html>` to cancel the wait operation.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
To check the current network connection, call :java-sdk:`getConnectionState()
<io/realm/mongodb/sync/SyncSession.html#getConnectionState()>` on your
:java-sdk:`SyncSession <io/realm/mongodb/sync/SyncSession.html>`.

You can also subscribe to connection changes on your ``SyncSession``
with :java-sdk:`addConnectionChangeListener()
<io/realm/mongodb/sync/SyncSession.html#addConnectionChangeListener(io.realm.mongodb.sync.ConnectionListener)>`
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To check the current Sync state, call :java-sdk:`getState()
<io/realm/mongodb/sync/SyncSession.html#getState()>` on your
:java-sdk:`SyncSession <io/realm/mongodb/sync/SyncSession.html>`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
You can access the session of any synced database through the
:java-sdk:`SyncSession <io/realm/mongodb/sync/SyncSession.html>`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
To pause a currently active sync session, call
:java-sdk:`stop() <io/realm/mongodb/sync/SyncSession.html#stop()>`
on your :java-sdk:`SyncSession <io/realm/mongodb/sync/SyncSession.html>`:

.. literalinclude:: /examples/generated/java/sync/SyncDataTest.snippet.pause-sync-session.java
:language: java
:copyable: false

To resume a currently paused sync session, call
:java-sdk:`start() <io/realm/mongodb/sync/SyncSession.html#start()>`
on your :java-sdk:`SyncSession <io/realm/mongodb/sync/SyncSession.html>`:
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
To pause a currently active sync session, call
:java-sdk:`stop() <io/realm/mongodb/sync/SyncSession.html#stop-->`
on your :java-sdk:`SyncSession <io/realm/mongodb/sync/SyncSession.html>`:

.. literalinclude:: /examples/generated/java/sync/SyncDataTest.snippet.pause-sync-session.kt
:language: kotlin
:copyable: false

To resume a currently paused sync session, call
:java-sdk:`start() <io/realm/mongodb/sync/SyncSession.html#start-->`
on your :java-sdk:`SyncSession <io/realm/mongodb/sync/SyncSession.html>`:
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
You can wait for changes to upload with :java-sdk:`SyncSession.uploadAllLocalChanges()
<io/realm/mongodb/sync/SyncSession.html#uploadAllLocalChanges()>` or
:java-sdk:`SyncSession.uploadAllLocalChanges(long timeout, TimeUnit unit)
<io/realm/mongodb/sync/SyncSession.html#uploadAllLocalChanges(long,java.util.concurrent.TimeUnit)>`.
These methods block block execution until all known changes on the device have
been uploaded to the server (or the specified timeout is hit).

You can wait for changes to download with :java-sdk:`SyncSession.downloadAllServerChanges()
<io/realm/mongodb/sync/SyncSession.html#downloadAllServerChanges()>` or
:java-sdk:`SyncSession.downloadAllServerChanges(long timeout, TimeUnit unit)
<io/realm/mongodb/sync/SyncSession.html#downloadAllServerChanges(long,java.util.concurrent.TimeUnit)>`.
These methods block block execution until all known changes on the server have
downloaded to the device (or the specified timeout is hit).
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To check the current state of the connection to the server, call the
:js-sdk:`syncSession.connectionState()
<classes/SyncSession.html#connectionState>` method.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
To check the current state of the Sync session, call the
:js-sdk:`syncSession.state() <classes/SyncSession.html#state>` method.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
To check the upload and download progress for a sync session, add a progress
notification using the :js-sdk:`syncSession.addProgressNotification()
<classes/SyncSession.html#addProgressNotification>` method.

The ``syncSession.addProgressNotification()`` method takes in the following
three parameters:

- A ``direction`` parameter.
Set to ``"upload"`` to register notifications for uploading data.
Set to ``"download"`` to register notifications for downloading data.
- A ``mode`` parameter. Set to ``"reportIndefinitely"``
for the notifications to continue until the callback is unregistered using
:js-sdk:`syncSession.removeProgressNotification() <classes/SyncSession.html#removeProgressNotification>`.
Set to ``"forCurrentlyOutstandingWork"`` for the notifications to continue
until only the currently transferable bytes are synced.
- A callback function parameter.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
After opening a synced database, you can access its sync session with the
:js-sdk:`Realm.syncSession <classes/Realm-1.html#syncSession>` property.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To attempt to manually reconnect all Sync sessions, call
:js-sdk:`Realm.App.Sync.Session.reconnect()
<classes/Realm.App.Sync.Session.html#reconnect>`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To pause synchronization, use the :js-sdk:`syncSession.pause()
<classes/SyncSession.html#pause>` method. To resume synchronization, use the
:js-sdk:`syncSession.resume() <classes/SyncSession.html#resume>` method.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
To asynchronously wait for all changes to upload to Atlas from your synced
database, call :js-sdk:`uploadAllLocalChanges()
<classes/SyncSession.html#uploadAllLocalChanges>`. This method returns
true when all changes have been uploaded.

To asynchronously wait for all changes on Atlas to download from the
Device Sync server to your synced database, call
:js-sdk:`downloadAllServerChanges() <classes/SyncSession.html#downloadAllServerChanges>`.
This method returns true when all changes have been downloaded.

You can specify a request timeout on the :js-sdk:`App configuration
<types/AppConfiguration.html>`. With a timeout specified, you can set
``cancelWaitsOnNonFatalErrors`` on your :js-sdk:`BaseSyncConfiguration
<types/BaseSyncConfiguration.html>`. When ``true`` and the timeout interval
arrives, any any outstanding work that is awaiting uploads and downloads
cancels. When this setting is false, awaiting uploads and downloads does
not cancel because the SDK treats these timeouts as non-fatal errors.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
You can check the network connection state through the
:kotlin-sync-sdk:`SyncSession.ConnectionState
<io.realm.kotlin.mongodb.sync/-connection-state/index.html>`. This is a
property of type ConnectionState enum, whose possible values are:

- DISCONNECTED
- CONNECTING
- CONNECTED

.. literalinclude:: /examples/generated/kotlin/ManageSyncSession.snippet.get-network-connection.kt
:language: kotlin

Monitor the state of the network connection with
:kotlin-sync-sdk:`connectionStateAsFlow
<io.realm.kotlin.mongodb.sync/-sync-session/connection-state-as-flow.html>`.
This property returns a Flow of :kotlin-sync-sdk:`ConnectionStateChange
<io.realm.kotlin.mongodb.sync/-connection-state-change/index.html>`
objects that updates when the network connection changes. You can access the
new and old ``ConnectionState`` from ``ConnectionStateChange``.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
You can check the Sync session state through the
:kotlin-sync-sdk:`SyncSession.State
<io.realm.kotlin.mongodb.sync/-sync-session/-state/index.html>`. This is a
property of type State enum, whose possible values are:

- INACTIVE
- ACTIVE
- PAUSED
- DYING
- WAITING_FOR_ACCESS_TOKEN
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
To monitor Sync upload and download progress, call
:kotlin-sync-sdk:`SyncSession.progressAsFlow()
<io.realm.kotlin.mongodb.sync/-sync-session/progress-as-flow.html>`.

This method returns a Flow of :kotlin-sync-sdk:`Progress
<io.realm.kotlin.mongodb.sync/-progress/index.html>` events. ``Progress``
provides an ``estimate``, a double, that represents a transfer progress
estimate that ranges from 0.0 to 1.0. It also provides an
``isTransferComplete`` bool.

``syncSession.progressAsFlow()`` takes two arguments:

- A :kotlin-sync-sdk:`Direction <io.realm.kotlin.mongodb.sync/-direction/index.html>`
enum that can be set to ``UPLOAD`` or ``DOWNLOAD``.
This specifies that the progress stream tracks uploads or downloads.

- A :kotlin-sync-sdk:`ProgressMode
<io.realm.kotlin.mongodb.sync/-progress-mode/index.html>` enum that can be
set to either:

- ``INDEFINITELY``: Sets notifications to continue until the callback is
unregistered.
- ``CURRENT_CHANGES``: Sets notifications to continue until only the currently
transferable bytes are synced.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
You can access the :kotlin-sync-sdk:`SyncSession
<io.realm.kotlin.mongodb.sync/-sync-session/index.html>`
of a single synced database through the :kotlin-sync-sdk:`realm.syncSession
<io.realm.kotlin.mongodb/sync-session.html>` property.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
In Kotlin SDK version 1.11.0 and later, you can choose to manually trigger a
reconnect attempt with the :kotlin-sync-sdk:`App.Sync.reconnect()
<io.realm.kotlin.mongodb.sync/-sync/reconnect.html>`
instead of waiting for the duration of the incremental backoff. This is
useful if you have a more accurate understanding of
the network conditions (for example, when monitoring network changes with the
``ConnectivityManager`` on Android) and don't want to rely on Realm's automatic
reconnect detection. The SDK also automatically calls this method when a device
toggles off airplane mode.
Loading
Loading