Check Upload and Download Progress - .NET SDK
On this page
Monitor Sync Progress and Status
You may want to know the status of Sync operations in your app. For example, you might want specific code to run only after all of the data is synced with App Services. You might also want to provide users with the status of Sync operations.
You can set up your Sync session to wait for changes to be uploaded or downloaded. You can also configure your Sync session to notify when the Sync connection status changes.
Wait for Changes to be Uploaded or Downloaded
To asynchronously wait for your changes to be completed, get the sync session from the Realms.Sync.SyncSession property, and then call the session.WaitForUploadAsync() or session.WaitForDownloadAsync() methods. For example:
using Realms.Sync; var realm = Realm.GetInstance(config); await realm.SyncSession.WaitForDownloadAsync();
Monitor Sync Progress
..versionchanged:: 12.0.0
Note
Flexible Sync progress notifications are not yet fully supported. When using Flexible Sync, downloads only report notifications after changes are integrated. Partition-Based Sync provides ongoing notifications as changes progress downloading. Uploads report ongoing progress notifications for both Sync Modes.
To monitor Sync progress, get the sync session from the Realms.Sync.SyncSession property, then add a progress notification by calling the session.GetProgressObservable() method.
The session.GetProgressObservable
method takes in the following two parameters:
A ProgressDirection parameter that can be set to
Upload
orDownload
.A ProgressMode parameter that can be set to
ReportIndefinitely
for the notifications to continue until the callback is unregistered, orForCurrentlyOutstandingWork
for the notifications to continue until only the currently transferable bytes are synced.
When you Subscribe to the notifications, you receive a SyncProgress object that provides an estimate of the percentage of remaining data to be transferred as a value between 0 and 1.0.
Example
In the following example, we subscribe to a progress
observable on the session
to listen for upload events. When
this callback is triggered, it prints the upload progress as a percentage.
var session = realm.SyncSession; var token = session.GetProgressObservable(ProgressDirection.Upload, ProgressMode.ReportIndefinitely) .Subscribe(progress => { Console.WriteLine($@"Current upload progress: {progress.ProgressEstimate * 100}%"); }); token.Dispose();
Once you no longer wish to receive notifications, unregister the token by using
token.Dispose()
.
Get Connection State Changes
To get the connection state of a SyncSession, set an event handler
on the PropertyChanged
event. The event handler is a standard
.NET PropertyChangedEventHandler delegate
that takes in a sender
object and
PropertyChangedEventArgs
object.
In the event handler, cast the sender to a Session
object and check if
the event argument's PropertyName
property is Session.ConnectionState
.
You can then get the
ConnectionState
value, which will be one of the following:
Connecting
Connected
Disconnected
The following code demonstrates setting the event handler, casting the session object, and checking the Sync status:
public void SetupRealm() { var appConfig = new AppConfiguration(myRealmAppId); app = App.Create(appConfig); user = app.LogInAsync(Credentials.Anonymous()).Result; config = new PartitionSyncConfiguration("myPartition", user); try { var realm = Realm.GetInstance(config); var session = realm.SyncSession; session.PropertyChanged += SyncSessionPropertyChanged!; realm.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } private void SyncSessionPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(Session.ConnectionState)) { var session = (Session)sender; var currentState = session.ConnectionState; if (currentState == ConnectionState.Connecting) { //session is connecting } if (currentState == ConnectionState.Connected) { //session is connected } if (currentState == ConnectionState.Disconnected) { //session has been disconnected } } }