Docs Menu

Class SyncSession

On this page

java.lang.Object
io.realm.mongodb.sync.SyncSession

A session controls how data is synchronized between a single Realm on the device and the server Realm on the Realm Object Server.A Session is created by opening a Realm instance using a SyncConfiguration . Once a session has been created, it will continue to exist until the app is closed or all threads using this SyncConfiguration closes their respective Realm s.

A session is controlled by Realm, but can provide additional information in case of errors. These errors are passed along in the SyncSession.ErrorHandler .

When creating a session, Realm will establish a connection to the server. This connection is controlled by Realm and might be shared between multiple sessions. It is possible to get insight into the connection using addConnectionChangeListener(ConnectionListener) and isConnected() .

The session itself has a different lifecycle than the underlying connection. The state of the session can be found using getState() .

The SyncSession object is thread safe.

Modifier and Type
Class and Description
public static final
public static interface
public static interface
Modifier and Type
Method and Description
public synchronized void

Adds a listener tracking changes to the connection backing this session.

public synchronized void

Adds a progress listener tracking changes that need to be downloaded from the Realm Object Server.

public synchronized void

Adds a progress listener tracking changes that need to be uploaded from the device to the Realm Object Server.

public boolean

Calling this method will block until all known remote changes have been downloaded and applied to the Realm or the specified timeout is hit.

public void

Calling this method will block until all known remote changes have been downloaded and applied to the Realm.

Returns the SyncConfiguration that is responsible for controlling the session.

Get the current state of the connection used by the session as defined in ConnectionState .

public URI

Returns the URI describing the remote Realm which this session connects to and synchronizes changes with.

Get the current session's state, as defined in SyncSession.State .

public User

Returns the User defined by the SyncConfiguration that is used to connect to MongoDB Realm.

public boolean

Checks if the session is connected to the server and can synchronize data.

public synchronized void

Removes a previously registered ConnectionListener .

public synchronized void

Removes a progress listener.

public synchronized void

Attempts to start the session and enable synchronization with the Realm Object Server.

public synchronized void
stop ()

Stops any synchronization with the Realm Object Server until the Realm is re-opened again after fully closing it.

public boolean
long timeout,
)

Calling this method will block until all known local changes have been uploaded to the server or the specified timeout is hit.

public void

Calling this method will block until all known local changes have been uploaded to the server.

  • Methods inherited from class java.lang.Object : getClass , hashCode , equals , clone , toString , notify , notifyAll , wait , wait , wait , finalize
public synchronized void addConnectionChangeListener (
)

Adds a listener tracking changes to the connection backing this session. See ConnectionState for further details.

Parameters

  • listener - the listener to register.

Throws

Tip
See also:
public synchronized void addDownloadProgressListener (
)

Adds a progress listener tracking changes that need to be downloaded from the Realm Object Server.The ProgressListener will be triggered immediately when registered, and periodically afterwards.

Parameters

  • mode - type of mode used. See ProgressMode for more information.
  • listener - the listener to register.
public synchronized void addUploadProgressListener (
)

Adds a progress listener tracking changes that need to be uploaded from the device to the Realm Object Server.The ProgressListener will be triggered immediately when registered, and periodically afterwards.

Parameters

  • mode - type of mode used. See ProgressMode for more information.
  • listener - the listener to register.
public boolean downloadAllServerChanges (
long timeout,
)

Calling this method will block until all known remote changes have been downloaded and applied to the Realm or the specified timeout is hit. This will involve network access, so calling this method should only be done from a non-UI thread.This method cannot be called before the Realm has been opened.

Returns

true if the data was downloaded before the timeout. false if the operation timed out or otherwise failed.

Throws

  • IllegalStateException - if called on the Android main thread.
  • InterruptedException - if the download took longer than the specified timeout or the thread was interrupted while downloading was in progress. The download will continue in the background even after this exception is thrown.
  • IllegalArgumentException - if timeout is less than or equal to 0 or unit is null .

Calling this method will block until all known remote changes have been downloaded and applied to the Realm. This will involve network access, so calling this method should only be done from a non-UI thread.If the device is offline, this method might never return.

This method cannot be called before the session has been started.

Throws

Returns the SyncConfiguration that is responsible for controlling the session.

Returns

SyncConfiguration that defines and controls this session.

Get the current state of the connection used by the session as defined in ConnectionState .

Returns

the state of connection used by the session.

Tip
See also:
public URI getServerUrl ()

Returns the URI describing the remote Realm which this session connects to and synchronizes changes with.

Returns

URI describing the remote Realm.

Get the current session's state, as defined in SyncSession.State .

Note that the state may change after this method returns.

Returns

the state of the session.

Tip
public User getUser ()

Returns the User defined by the SyncConfiguration that is used to connect to MongoDB Realm.

Returns

User used to authenticate the session on MongoDB Realm.

public boolean isConnected ()

Checks if the session is connected to the server and can synchronize data. This is a best guess effort. To conserve battery the underlying implementation uses heartbeats to detect if the connection is still available. So if no data is actively being synced and some time has elapsed since the last heartbeat, the connection could have been dropped but this method will still return true .

Returns

true if the session is connected and ready to synchronize data, false if not or if it is in the process of connecting.

public synchronized void removeConnectionChangeListener (
)

Removes a previously registered ConnectionListener .

Parameters

  • listener - listener to remove

Throws

public synchronized void removeProgressListener (
)

Removes a progress listener. If the listener wasn't registered, this method will do nothing.

Parameters

  • listener - listener to remove.
public synchronized void start ()

Attempts to start the session and enable synchronization with the Realm Object Server.This happens automatically when opening the Realm instance, so doing it manually should only be needed if the session was stopped using stop() .

If the session was already started, calling this method will do nothing.

A session is considered started if getState() returns State.ACTIVE . If the session is State.DYING , the session will be moved back to State.ACTIVE .

Tip
public synchronized void stop ()

Stops any synchronization with the Realm Object Server until the Realm is re-opened again after fully closing it.Synchronization can be re-enabled by calling start() again.

If the session is already stopped, calling this method will do nothing.

public boolean uploadAllLocalChanges (
long timeout,
)

Calling this method will block until all known local changes have been uploaded to the server or the specified timeout is hit. This will involve network access, so calling this method should only be done from a non-UI thread.This method cannot be called before the Realm has been opened.

Returns

true if the data was uploaded before the timeout. false if the operation timed out or otherwise failed.

Throws

  • IllegalStateException - if called on the Android main thread.
  • InterruptedException - if the upload took longer than the specified timeout or the thread was interrupted while uploading was in progress. The upload will continue in the background even after this exception is thrown.
  • IllegalArgumentException - if timeout is less than or equal to 0 or unit is null .
public void uploadAllLocalChanges ()

Calling this method will block until all known local changes have been uploaded to the server. This will involve network access, so calling this method should only be done from a non-UI thread.If the device is offline, this method might never return.

This method cannot be called before the Realm has been opened.

Throws

←  Class SyncConfiguration.BuilderInterface SyncSession.ClientResetHandler →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.