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.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.
|
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 . |
SyncUser |
getUser()
Returns the
SyncUser defined by the SyncConfiguration that is used to connect to the
Realm Object Server. |
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.
|
public SyncConfiguration getConfiguration()
SyncConfiguration
that is responsible for controlling the session.public SyncUser getUser()
SyncUser
defined by the SyncConfiguration
that is used to connect to the
Realm Object Server.SyncUser
used to authenticate the session on the Realm Object Server.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, example: the authentication
token will expire, causing the session to move to SyncSession.State.WAITING_FOR_ACCESS_TOKEN
after it was in SyncSession.State.ACTIVE
.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 void uploadAllLocalChanges() 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 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 either SyncSession.State.ACTIVE
or
SyncSession.State.WAITING_FOR_ACCESS_TOKEN
. 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.