Class SyncConfiguration.Builder
- java.lang.Object
-
- io.realm.mongodb.sync.SyncConfiguration.Builder
-
- Enclosing class:
- SyncConfiguration
public static final class SyncConfiguration.Builder extends Object
Builder used to construct instances of a SyncConfiguration in a fluent manner.
-
-
Constructor Summary
Constructors Constructor Description Builder(User user)
Creates an instance of the builder for a Flexible Sync SyncConfiguration with the given user.Builder(User user, Integer partitionValue)
Creates an instance of the builder for a Partition-based Sync SyncConfiguration with the given user and partition value.Builder(User user, Long partitionValue)
Creates an instance of the builder for a Partition-based Sync SyncConfiguration with the given user and partition value.Builder(User user, String partitionValue)
Creates an instance of the builder for a Partition-based Sync SyncConfiguration with the given user and partition value.Builder(User user, UUID partitionValue)
Creates an instance of the builder for a Partition-based Sync SyncConfiguration with the given user and partition value.Builder(User user, ObjectId partitionValue)
Creates an instance of the builder for a Partition-based Sync SyncConfiguration with the given user and partition value.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description SyncConfiguration.Builder
addModule(Object module)
Adds a module to the already defined modules.SyncConfiguration.Builder
allowQueriesOnUiThread(boolean allowQueriesOnUiThread)
Sets whether or notRealmQueries
are allowed from the UI thread.SyncConfiguration.Builder
allowWritesOnUiThread(boolean allowWritesOnUiThread)
Sets whether or not calls toRealm.executeTransaction(io.realm.Realm.Transaction)
are allowed from the UI thread.SyncConfiguration.Builder
assetFile(String assetFile)
When opening the Realm for the first time, instead of creating an empty file, the Realm file will be copied from the provided asset file and used instead.SyncConfiguration
build()
Creates the RealmConfiguration based on the builder parameters.SyncConfiguration.Builder
clientResetHandler(SyncSession.ClientResetHandler handler)
Deprecated.SyncConfiguration.Builder
compactOnLaunch()
Setting this will cause Realm to compact the Realm file if the Realm file has grown too large and a significant amount of space can be recovered.SyncConfiguration.Builder
compactOnLaunch(CompactOnLaunchCallback compactOnLaunch)
Sets this to determine if the Realm file should be compacted before returned to the user.SyncConfiguration.Builder
encryptionKey(byte[] key)
Sets the 64 bytes key used to encrypt and decrypt the Realm file.SyncConfiguration.Builder
errorHandler(SyncSession.ErrorHandler errorHandler)
Sets the error handler used by this configuration.SyncConfiguration.Builder
flowFactory(FlowFactory factory)
Sets theFlowFactory
used to create coroutines Flows from Realm objects.SyncConfiguration.Builder
initialData(Realm.Transaction transaction)
Sets the initial data inRealm
.SyncConfiguration.Builder
initialSubscriptions(SyncConfiguration.InitialFlexibleSyncSubscriptions action)
Sets the initialSubscription
s for theRealm
.SyncConfiguration.Builder
inMemory()
Setting this will create an in-memory Realm instead of saving it to disk.SyncConfiguration.Builder
maxNumberOfActiveVersions(long number)
Sets the maximum number of live versions in the Realm file before anIllegalStateException
is thrown when attempting to write more data.SyncConfiguration.Builder
modules(Iterable<Object> modules)
Replaces the existing module(s) with one or moreRealmModule
s.SyncConfiguration.Builder
modules(Object baseModule, Object... additionalModules)
Replaces the existing module(s) with one or moreRealmModule
s.SyncConfiguration.Builder
name(String filename)
Sets the filename for the Realm file on this device.SyncConfiguration.Builder
readOnly()
Setting this will cause the Realm to become read only and all write transactions made against this Realm will fail with anIllegalStateException
.SyncConfiguration.Builder
rxFactory(RxObservableFactory factory)
Sets theRxObservableFactory
used to create Rx Observables from Realm objects.SyncConfiguration.Builder
schemaVersion(long schemaVersion)
Sets the schema version of the Realm.SyncConfiguration.Builder
syncClientResetStrategy(DiscardUnsyncedChangesStrategy handler)
Sets the handler for when a Client Reset occurs.SyncConfiguration.Builder
syncClientResetStrategy(ManuallyRecoverUnsyncedChangesStrategy handler)
Sets the handler for when a Client Reset occurs.SyncConfiguration.Builder
urlPrefix(String urlPrefix)
The prefix that is prepended to the path in the WebSocket request that initiates a sync connection to MongoDB Realm.SyncConfiguration.Builder
waitForInitialRemoteData()
Setting this will cause the Realm to download all known changes from the server the first time a Realm is opened.SyncConfiguration.Builder
waitForInitialRemoteData(long timeout, TimeUnit unit)
Setting this will cause the Realm to download all known changes from the server the first time a Realm is opened.
-
-
-
Constructor Detail
-
Builder
public Builder(User user)
Creates an instance of the builder for a Flexible Sync SyncConfiguration with the given user.- Parameters:
user
- The user that will be used for accessing the Realm App.
-
Builder
public Builder(User user, @Nullable String partitionValue)
Creates an instance of the builder for a Partition-based Sync SyncConfiguration with the given user and partition value.- Parameters:
user
- The user that will be used for accessing the Realm App.partitionValue
- The partition value identifying the remote Realm that will be synchronized.
-
Builder
public Builder(User user, @Nullable ObjectId partitionValue)
Creates an instance of the builder for a Partition-based Sync SyncConfiguration with the given user and partition value.- Parameters:
user
- The user that will be used for accessing the Realm App.partitionValue
- The partition value identifying the remote Realm that will be synchronized.
-
Builder
public Builder(User user, @Nullable Integer partitionValue)
Creates an instance of the builder for a Partition-based Sync SyncConfiguration with the given user and partition value.- Parameters:
user
- The user that will be used for accessing the Realm App.partitionValue
- The partition value identifying the remote Realm that will be synchronized.
-
Builder
public Builder(User user, @Nullable Long partitionValue)
Creates an instance of the builder for a Partition-based Sync SyncConfiguration with the given user and partition value.- Parameters:
user
- The user that will be used for accessing the Realm App.partitionValue
- The partition value identifying the remote Realm that will be synchronized.
-
Builder
public Builder(User user, @Nullable UUID partitionValue)
Creates an instance of the builder for a Partition-based Sync SyncConfiguration with the given user and partition value.- Parameters:
user
- The user that will be used for accessing the Realm App.partitionValue
- The partition value identifying the remote Realm that will be synchronized.
-
-
Method Detail
-
name
public SyncConfiguration.Builder name(String filename)
Sets the filename for the Realm file on this device.- Parameters:
filename
- name for the Realm file.
-
encryptionKey
public SyncConfiguration.Builder encryptionKey(byte[] key)
Sets the 64 bytes key used to encrypt and decrypt the Realm file.- Parameters:
key
- the encryption key.- Throws:
IllegalArgumentException
- if key is invalid.
-
schemaVersion
public SyncConfiguration.Builder schemaVersion(long schemaVersion)
Sets the schema version of the Realm.Synced Realms only support additive schema changes which can be applied without requiring a manual migration. The schema version will only be used as an indication to the underlying storage layer to remove or add indexes. These will be recalculated if the provided schema version differ from the version in the Realm file. WARNING: There is no guarantee that the value inserted here is the same returned by
BaseRealm.getVersion()
. Due to the nature of synced Realms, the value can both be higher and lower.- It will be lower if another client with a lesser
schemaVersion
connected to the server for the first time after this schemaVersion was used. - It will be higher if another client with a higher
schemaVersion
connected to the server after this Realm was created.
- Parameters:
schemaVersion
- the schema version.- Throws:
IllegalArgumentException
- if schema version is invalid.
- It will be lower if another client with a lesser
-
modules
public SyncConfiguration.Builder modules(Object baseModule, Object... additionalModules)
Replaces the existing module(s) with one or moreRealmModule
s. Using this method will replace the current schema for this Realm with the schema defined by the provided modules.A reference to the default Realm module containing all Realm classes in the project (but not dependencies), can be found using
Realm.getDefaultModule()
. Combining the schema from the app project and a library dependency is thus done using the following code:builder.modules(Realm.getDefaultMode(), new MyLibraryModule());
- Parameters:
baseModule
- the first Realm module (required).additionalModules
- the additional Realm modules- Throws:
IllegalArgumentException
- if any of the modules don't have theRealmModule
annotation.- See Also:
Realm.getDefaultModule()
-
modules
public SyncConfiguration.Builder modules(Iterable<Object> modules)
Replaces the existing module(s) with one or moreRealmModule
s. Using this method will replace the current schema for this Realm with the schema defined by the provided modules.A reference to the default Realm module containing all Realm classes in the project (but not dependencies), can be found using
Realm.getDefaultModule()
. Combining the schema from the app project and a library dependency is thus done using the following code:builder.modules(Realm.getDefaultMode(), new MyLibraryModule());
- Parameters:
modules
- list of modules tthe first Realm module (required).- Throws:
IllegalArgumentException
- if any of the modules don't have theRealmModule
annotation.- See Also:
Realm.getDefaultModule()
-
addModule
public SyncConfiguration.Builder addModule(Object module)
Adds a module to the already defined modules.
-
rxFactory
public SyncConfiguration.Builder rxFactory(@Nonnull RxObservableFactory factory)
Sets theRxObservableFactory
used to create Rx Observables from Realm objects. The default factory isRealmObservableFactory
.- Parameters:
factory
- factory to use.
-
flowFactory
public SyncConfiguration.Builder flowFactory(@Nonnull FlowFactory factory)
Sets theFlowFactory
used to create coroutines Flows from Realm objects. The default factory isRealmFlowFactory
.- Parameters:
factory
- factory to use.
-
initialData
public SyncConfiguration.Builder initialData(Realm.Transaction transaction)
Sets the initial data inRealm
. This transaction will be executed only the first time the Realm file is opened (created) or while migrating the data ifRealmConfiguration.Builder.deleteRealmIfMigrationNeeded()
is set.- Parameters:
transaction
- transaction to execute.
-
initialSubscriptions
public SyncConfiguration.Builder initialSubscriptions(SyncConfiguration.InitialFlexibleSyncSubscriptions action)
Sets the initialSubscription
s for theRealm
. This will only be executed the first time the Realm file is opened (and the file created). IfwaitForInitialRemoteData()
is configured as well, the realm file isn't fully opened until all subscription data also has been uploaded.- Parameters:
action
-MutableSubscriptionSet
where subscriptions can be added.
-
inMemory
public SyncConfiguration.Builder inMemory()
Setting this will create an in-memory Realm instead of saving it to disk. In-memory Realms might still use disk space if memory is running low, but all files created by an in-memory Realm will be deleted when the Realm is closed.Note that because in-memory Realms are not persisted, you must be sure to hold on to at least one non-closed reference to the in-memory Realm object with the specific name as long as you want the data to last.
-
errorHandler
public SyncConfiguration.Builder errorHandler(SyncSession.ErrorHandler errorHandler)
Sets the error handler used by this configuration.Only errors not handled by the defined
SyncPolicy
will be reported to this error handler.- Parameters:
errorHandler
- error handler used to report back errors when communicating with the Realm Object Server.- Throws:
IllegalArgumentException
- ifnull
is given as an error handler.
-
clientResetHandler
@Deprecated public SyncConfiguration.Builder clientResetHandler(SyncSession.ClientResetHandler handler)
Deprecated.Sets the handler for when a Client Reset occurs. If no handler is set, and error is logged when a Client Reset occurs.- Parameters:
handler
- custom handler in case of a Client Reset.
-
syncClientResetStrategy
public SyncConfiguration.Builder syncClientResetStrategy(ManuallyRecoverUnsyncedChangesStrategy handler)
Sets the handler for when a Client Reset occurs. If no handler is set, and error is logged when a Client Reset occurs.- Parameters:
handler
- custom manual handler in case of a Client Reset.
-
syncClientResetStrategy
public SyncConfiguration.Builder syncClientResetStrategy(DiscardUnsyncedChangesStrategy handler)
Sets the handler for when a Client Reset occurs. If no handler is set, and error is logged when a Client Reset occurs. This strategy is only available for synced realms using partition based sync. Realms using flexible sync currently only supportsyncClientResetStrategy(ManuallyRecoverUnsyncedChangesStrategy)
.- Parameters:
handler
- custom seamless loss handler in case of a Client Reset.
-
waitForInitialRemoteData
public SyncConfiguration.Builder waitForInitialRemoteData()
Setting this will cause the Realm to download all known changes from the server the first time a Realm is opened. The Realm will not open until all the data has been downloaded. This means that if a device is offline the Realm will not open.Since downloading all changes can be an lengthy operation that might block the UI thread, Realms with this setting enabled should only be opened on background threads or with
Realm.getInstanceAsync(RealmConfiguration, Realm.Callback)
on the UI thread.This check is only enforced the first time a Realm is created. If you otherwise want to make sure a Realm has the latest changes, use
SyncSession.downloadAllServerChanges()
.
-
waitForInitialRemoteData
public SyncConfiguration.Builder waitForInitialRemoteData(long timeout, TimeUnit unit)
Setting this will cause the Realm to download all known changes from the server the first time a Realm is opened. The Realm will not open until all the data has been downloaded. This means that if a device is offline the Realm will not open.Since downloading all changes can be an lengthy operation that might block the UI thread, Realms with this setting enabled should only be opened on background threads or with
Realm.getInstanceAsync(RealmConfiguration, Realm.Callback)
on the UI thread.This check is only enforced the first time a Realm is created. If you otherwise want to make sure a Realm has the latest changes, use
SyncSession.downloadAllServerChanges()
.- Parameters:
timeout
- how long to wait for the download to complete before anDownloadingRealmInterruptedException
is thrown.unit
- the unit of time used to define the timeout.
-
readOnly
public SyncConfiguration.Builder readOnly()
Setting this will cause the Realm to become read only and all write transactions made against this Realm will fail with anIllegalStateException
.This in particular mean that
Marking a Realm as read only only applies to the Realm in this process. Other processes and devices can still write to the Realm.initialData(Realm.Transaction)
will not work in combination with a read only Realm and setting this will result in aIllegalStateException
being thrown.
-
compactOnLaunch
public SyncConfiguration.Builder compactOnLaunch()
Setting this will cause Realm to compact the Realm file if the Realm file has grown too large and a significant amount of space can be recovered. SeeDefaultCompactOnLaunchCallback
for details.
-
compactOnLaunch
public SyncConfiguration.Builder compactOnLaunch(CompactOnLaunchCallback compactOnLaunch)
Sets this to determine if the Realm file should be compacted before returned to the user. It is passed the total file size (data + free space) and the bytes used by data in the file.- Parameters:
compactOnLaunch
- a callback called when opening a Realm for the first time during the life of a process to determine if it should be compacted before being returned to the user. It is passed the total file size (data + free space) and the bytes used by data in the file.
-
assetFile
public SyncConfiguration.Builder assetFile(String assetFile)
When opening the Realm for the first time, instead of creating an empty file, the Realm file will be copied from the provided asset file and used instead.This can be used to pre-populate the Realm with data, so it doesn't have to be downloaded from the server.
The provided Realm file must be a valid synced Realm for the given user, and it must have been created using the
BaseRealm.writeCopyTo(File)
API.WARNING: This could potentially be a lengthy operation and should ideally be done on a background thread.
- Parameters:
assetFile
- path to the asset database file.- Throws:
IllegalStateException
- if this Realm is also marked asinMemory()
.
-
urlPrefix
public SyncConfiguration.Builder urlPrefix(String urlPrefix)
The prefix that is prepended to the path in the WebSocket request that initiates a sync connection to MongoDB Realm. The value specified must match the server’s configuration otherwise the device will not be able to create a connection. This value is optional and should only be set if a specific firewall rule requires it.- Parameters:
urlPrefix
- The prefix to append to the sync connection url.
-
maxNumberOfActiveVersions
public SyncConfiguration.Builder maxNumberOfActiveVersions(long number)
Sets the maximum number of live versions in the Realm file before anIllegalStateException
is thrown when attempting to write more data.Realm is capable of concurrently handling many different versions of Realm objects. This can happen if you have a Realm open on many different threads or are freezing objects while data is being written to the file.
Under normal circumstances this is not a problem, but if the number of active versions grow too large, it will have a negative effect on the filesize on disk. Setting this parameters can therefore be used to prevent uses of Realm that can result in very large Realms.
Note, the version number will also increase when changes from other devices are integrated on this device, so the number of active versions will also depend on what other devices writing to the same Realm are doing.
- Parameters:
number
- the maximum number of active versions before an exception is thrown.- See Also:
- FAQ
-
allowWritesOnUiThread
public SyncConfiguration.Builder allowWritesOnUiThread(boolean allowWritesOnUiThread)
Sets whether or not calls toRealm.executeTransaction(io.realm.Realm.Transaction)
are allowed from the UI thread.WARNING: Realm does not allow synchronous transactions to be run on the main thread unless users explicitly opt in with this method. We recommend diverting calls to
executeTransaction
to non-UI threads or, alternatively, usingRealm.executeTransactionAsync(io.realm.Realm.Transaction)
.
-
allowQueriesOnUiThread
public SyncConfiguration.Builder allowQueriesOnUiThread(boolean allowQueriesOnUiThread)
Sets whether or notRealmQueries
are allowed from the UI thread.By default Realm allows queries on the main thread. However, by doing so your application may experience a drop of frames or even ANRs. We recommend diverting queries to non-UI threads or, alternatively, using
RealmQuery.findAllAsync()
orRealmQuery.findFirstAsync()
.
-
build
public SyncConfiguration build()
Creates the RealmConfiguration based on the builder parameters.- Returns:
- the created
SyncConfiguration
. - Throws:
IllegalStateException
- if the configuration parameters are invalid or inconsistent.
-
-