Class Session
An object encapsulating a synchronization session. Sessions represent the communication between the client (and a local Realm file on disk), and MongoDB Atlas. Sessions are always created by the SDK and vended out through various APIs. The lifespans of sessions associated with Realms are managed automatically.
Implements
Namespace: Realms.Sync
Assembly: Realm.dll
Syntax
public class Session : INotifyPropertyChanged
Properties
| Edit this page View SourceConnectionState
Gets the session’s current connection state.
Declaration
public ConnectionState ConnectionState { get; }
Property Value
Type | Description |
---|---|
ConnectionState | An enum value indicating the connection state of the session. |
Path
Gets the on-disk path of the Realm file backing the Realm this Session represents.
Declaration
public string Path { get; }
Property Value
Type | Description |
---|---|
string | The file path. |
State
Gets the session’s current state.
Declaration
public SessionState State { get; }
Property Value
Type | Description |
---|---|
SessionState | An enum value indicating the state of the session. |
User
Gets the User defined by the SyncConfigurationBase that is used to connect to MongoDB Atlas.
Declaration
public User User { get; }
Property Value
Type | Description |
---|---|
User | The User that was used to create the Realm's SyncConfigurationBase. |
Methods
| Edit this page View SourceGetProgressObservable(ProgressDirection, ProgressMode)
Gets an IObservable<T> that can be used to track upload or download progress.
Declaration
public IObservable<SyncProgress> GetProgressObservable(ProgressDirection direction, ProgressMode mode)
Parameters
Type | Name | Description |
---|---|---|
ProgressDirection | direction | The transfer direction (upload or download) to track in the subscription callback. |
ProgressMode | mode | The desired behavior of this progress notification block. |
Returns
Type | Description |
---|---|
IObservable<SyncProgress> | An observable that you can subscribe to and receive progress updates. |
Remarks
To start receiving notifications, you should call Subscribe(IObserver<T>) on the returned object. The token returned from Subscribe(IObserver<T>) should be retained as long as progress notifications are desired. To stop receiving notifications, call Dispose() on the token. You don't need to keep a reference to the observable itself. The progress callback will always be called once immediately upon subscribing in order to provide the latest available status information.
Examples
class ProgressNotifyingViewModel
{
private IDisposable notificationToken;
public void ShowProgress()
{
var observable = session.GetProgressObservable(ProgressDirection.Upload, ProgressMode.ReportIndefinitely);
notificationToken = observable.Subscribe(progress =>
{
// Update relevant properties by accessing progress.ProgressEstimate
});
}
public void HideProgress()
{
notificationToken?.Dispose();
notificationToken = null;
}
}
In this example we're using ObservableExtensions.Subscribe found in the Reactive Extensions class library. If you prefer not to take a dependency on it, you can create a class that implements IObserver<T> and use it to subscribe instead.
| Edit this page View SourceStart()
Attempts to resume the session and enable synchronization with the server.
Declaration
public void Start()
Remarks
All sessions will be active by default and calling this method only makes sense if Stop() was called before that.
Stop()
Stops any synchronization with the server until the Realm is re-opened again
after fully closing it.
Synchronization can be re-enabled by calling Start() again.
Declaration
public void Stop()
Remarks
If the session is already stopped, calling this method will do nothing.
WaitForDownloadAsync(CancellationToken?)
Waits for the Session to finish all pending downloads.
Declaration
public Task WaitForDownloadAsync(CancellationToken? cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken? | cancellationToken | An optional cancellation token that can be used to cancel the wait operation. |
Returns
Type | Description |
---|---|
Task | An awaitable Task that will be completed when all pending downloads for this Session are completed. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown when a faulted session is waited on. |
WaitForUploadAsync(CancellationToken?)
Waits for the Session to finish all pending uploads.
Declaration
public Task WaitForUploadAsync(CancellationToken? cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken? | cancellationToken | An optional cancellation token that can be used to cancel the wait operation. |
Returns
Type | Description |
---|---|
Task | An awaitable Task that will be completed when all pending uploads for this Session are completed. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown when a faulted session is waited on. |
Events
| Edit this page View SourcePropertyChanged
Occurs when a property value changes.
Declaration
public event PropertyChangedEventHandler? PropertyChanged
Event Type
Type | Description |
---|---|
PropertyChangedEventHandler |