public static final class SyncConfiguration.Builder extends Object
Constructor and Description |
---|
Builder(SyncUser user,
String uri)
Creates an instance of the Builder for the SyncConfiguration.
|
Modifier and Type | Method and Description |
---|---|
SyncConfiguration |
build()
Creates the RealmConfiguration based on the builder parameters.
|
SyncConfiguration.Builder |
directory(File directory)
Sets the local root directory where synchronized Realm files can be saved.
|
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 |
initialData(Realm.Transaction transaction)
Sets the initial data in
Realm . |
SyncConfiguration.Builder |
inMemory()
Setting this will create an in-memory Realm instead of saving it to disk.
|
SyncConfiguration.Builder |
modules(Object baseModule,
Object... additionalModules)
Replaces the existing module(s) with one or more
RealmModule s. |
SyncConfiguration.Builder |
name(String filename)
Sets the local file name for the Realm.
|
SyncConfiguration.Builder |
readOnly()
Setting this will cause the Realm to become read only and all write transactions made against this Realm will
fail with an
IllegalStateException . |
SyncConfiguration.Builder |
rxFactory(RxObservableFactory factory)
Sets the
RxObservableFactory used to create Rx Observables from Realm objects. |
SyncConfiguration.Builder |
schemaVersion(long schemaVersion)
Sets the schema version of the 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.
|
public Builder(SyncUser user, String uri)
Opening a synchronized Realm requires a valid user and an unique URI that identifies that Realm. In URIs,
/~/
can be used as a placeholder for a user ID in case the Realm should only be available to one
user e.g., "realm://objectserver.realm.io/~/default"
.
The URL cannot end with .realm
, .realm.lock
or .realm.management
.
The /~/
will automatically be replaced with the user ID when creating the SyncConfiguration
.
Moreover, the URI defines the local location on disk. The default location of a synchronized Realm file is
/data/data/<packageName>/files/realm-object-server/<user-id>/<last-path-segment>
, but this behavior
can be overwritten using name(String)
and directory(File)
.
Many Android devices are using FAT32 file systems. FAT32 file systems have a limitation that file names cannot be longer than 255 characters. Moreover, the entire URI should not exceed 256 characters. If file name and underlying path are too long to handle for FAT32, a shorter unique name will be generated. See also @{link https://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx}.
user
- the user for this Realm. An authenticated SyncUser
is required to open any Realm managed
by a Realm Object Server.uri
- URI identifying the Realm. If only a path like /~/default
is given, the configuration will
assume the file is located on the same server returned by SyncUser.getAuthenticationUrl()
.SyncUser.isValid()
public SyncConfiguration.Builder name(String filename)
filename
- name of the local file on disk.IllegalArgumentException
- if file name is null
or empty.public SyncConfiguration.Builder directory(File directory)
Synchronized Realms will not be saved directly in the provided directory, but instead in a subfolder that matches the path defined by Realm URI. As Realm server URIs are unique this means that multiple users can save their Realms on disk without the risk of them overwriting each other files.
The default location is context.getFilesDir()
.
directory
- directory on disk where the Realm file can be saved.IllegalArgumentException
- if the directory is not valid.public SyncConfiguration.Builder encryptionKey(byte[] key)
key
- the encryption key.IllegalArgumentException
- if key is invalid.public SyncConfiguration.Builder schemaVersion(long schemaVersion)
While synced Realms only support additive schema changes which can be applied without requiring a manual migration, the schema version must still be incremented as an indication to Realm that the change was intentional.
Failing to increment the schema version will cause Realm to throw a RealmMigrationNeededException
when the Realm is opened and the changed schema will not be applied.
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.
schemaVersion
connected to the server for
the first time after this schemaVersion was used.
schemaVersion
connected to the server after
this Realm was created.
schemaVersion
- the schema version.IllegalArgumentException
- if schema version is invalid.public SyncConfiguration.Builder modules(Object baseModule, Object... additionalModules)
RealmModule
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());
baseModule
- the first Realm module (required).additionalModules
- the additional Realm modulesIllegalArgumentException
- if any of the modules don't have the RealmModule
annotation.Realm.getDefaultModule()
public SyncConfiguration.Builder rxFactory(RxObservableFactory factory)
RxObservableFactory
used to create Rx Observables from Realm objects.
The default factory is RealmObservableFactory
.factory
- factory to use.public SyncConfiguration.Builder initialData(Realm.Transaction transaction)
Realm
. This transaction will be executed only the first time
the Realm file is opened (created) or while migrating the data if
RealmConfiguration.Builder.deleteRealmIfMigrationNeeded()
is set.transaction
- transaction to execute.public SyncConfiguration.Builder inMemory()
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.
public SyncConfiguration.Builder errorHandler(SyncSession.ErrorHandler errorHandler)
SyncManager.setDefaultSessionErrorHandler(SyncSession.ErrorHandler)
.
Only errors not handled by the defined SyncPolicy
will be reported to this error handler.
errorHandler
- error handler used to report back errors when communicating with the Realm Object Server.IllegalArgumentException
- if null
is given as an error handler.public SyncConfiguration.Builder waitForInitialRemoteData()
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()
.
public SyncConfiguration.Builder readOnly()
IllegalStateException
.
This in particular mean that initialData(Realm.Transaction)
will not work in combination with a
read only Realm and setting this will result in a IllegalStateException
being thrown.
public SyncConfiguration build()
SyncConfiguration
.IllegalStateException
- if the configuration parameters are invalid or inconsistent.