public class SyncSession extends Object
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 |
---|---|
static interface |
SyncSession.ClientResetHandler
Callback for the specific error event known as a Client Reset, determined by the error code
ErrorCode.CLIENT_RESET . |
static interface |
SyncSession.ErrorHandler
Interface used to report any session errors.
|
static class |
SyncSession.State
Enum describing the states a SyncSession can be in.
|
Modifier and Type | Method and Description |
---|---|
void |
addConnectionChangeListener(ConnectionListener listener)
Adds a listener tracking changes to the connection backing this session.
|
void |
addDownloadProgressListener(ProgressMode mode,
ProgressListener listener)
Adds a progress listener tracking changes that need to be downloaded from the Realm Object
Server.
|
void |
addUploadProgressListener(ProgressMode mode,
ProgressListener listener)
Adds a progress listener tracking changes that need to be uploaded from the device to the
Realm Object Server.
|
void |
downloadAllServerChanges()
Calling this method will block until all known remote changes have been downloaded and applied to the Realm.
|
boolean |
downloadAllServerChanges(long timeout,
TimeUnit unit)
Calling this method will block until all known remote changes have been downloaded and applied to the Realm
or the specified timeout is hit.
|
SyncConfiguration |
getConfiguration()
Returns the
SyncConfiguration that is responsible for controlling the session. |
ConnectionState |
getConnectionState()
Get the current state of the connection used by the session as defined in
ConnectionState . |
URI |
getServerUrl()
Returns the
URI describing the remote Realm which this session connects to and synchronizes changes with. |
SyncSession.State |
getState()
Get the current session's state, as defined in
SyncSession.State . |
User |
getUser()
Returns the
User defined by the SyncConfiguration that is used to connect to
MongoDB Realm. |
boolean |
isConnected()
Checks if the session is connected to the server and can synchronize data.
|
void |
removeConnectionChangeListener(ConnectionListener listener)
Removes a previously registered
ConnectionListener . |
void |
removeProgressListener(ProgressListener listener)
Removes a progress listener.
|
void |
start()
Attempts to start the session and enable synchronization with the Realm Object Server.
|
void |
stop()
Stops any synchronization with the Realm Object Server until the Realm is re-opened again
after fully closing it.
|
void |
uploadAllLocalChanges()
Calling this method will block until all known local changes have been uploaded to the server.
|
boolean |
uploadAllLocalChanges(long timeout,
TimeUnit unit)
Calling this method will block until all known local changes have been uploaded to the server or the specified
timeout is hit.
|
public SyncConfiguration getConfiguration()
SyncConfiguration
that is responsible for controlling the session.public User getUser()
User
defined by the SyncConfiguration
that is used to connect to
MongoDB Realm.User
used to authenticate the session on MongoDB Realm.public URI getServerUrl()
URI
describing the remote Realm which this session connects to and synchronizes changes with.URI
describing the remote Realm.public SyncSession.State getState()
SyncSession.State
.
Note that the state may change after this method returns.
SyncSession.State
public ConnectionState getConnectionState()
ConnectionState
.ConnectionState
public boolean isConnected()
true
.true
if the session is connected and ready to synchronize data, false
if not or if it is in the process of connecting.public void addDownloadProgressListener(ProgressMode mode, ProgressListener listener)
The ProgressListener
will be triggered immediately when registered, and periodically
afterwards.
mode
- type of mode used. See ProgressMode
for more information.listener
- the listener to register.public void addUploadProgressListener(ProgressMode mode, ProgressListener listener)
The ProgressListener
will be triggered immediately when registered, and periodically
afterwards.
mode
- type of mode used. See ProgressMode
for more information.listener
- the listener to register.public void removeProgressListener(ProgressListener listener)
listener
- listener to remove.public void addConnectionChangeListener(ConnectionListener listener)
ConnectionState
for further details.listener
- the listener to register.IllegalArgumentException
- if the listener is null
.ConnectionState
public void removeConnectionChangeListener(ConnectionListener listener)
ConnectionListener
.listener
- listener to removeIllegalArgumentException
- if the listener is null
.public void downloadAllServerChanges() throws InterruptedException
If the device is offline, this method might never return.
This method cannot be called before the session has been started.
IllegalStateException
- if called on the Android main thread.InterruptedException
- if the thread was interrupted while downloading was in progress.public boolean downloadAllServerChanges(long timeout, TimeUnit unit) throws InterruptedException
This method cannot be called before the Realm has been opened.
true
if the data was downloaded before the timeout. false
if the operation timed out or otherwise failed.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
.public void uploadAllLocalChanges() throws InterruptedException
If the device is offline, this method might never return.
This method cannot be called before the Realm has been opened.
IllegalStateException
- if called on the Android main thread.InterruptedException
- if the thread was interrupted while downloading was in progress.public boolean uploadAllLocalChanges(long timeout, TimeUnit unit) throws InterruptedException
This method cannot be called before the Realm has been opened.
true
if the data was uploaded before the timeout. false
if the operation timed out or otherwise failed.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 start()
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 SyncSession.State.ACTIVE
.
If the session is SyncSession.State.DYING
, the session
will be moved back to SyncSession.State.ACTIVE
.
getState()
,
stop()
public void stop()
Synchronization can be re-enabled by calling start()
again.
If the session is already stopped, calling this method will do nothing.