Class RealmConfiguration.Builder
- java.lang.Object
-
- io.realm.RealmConfiguration.Builder
-
- Enclosing class:
- RealmConfiguration
public static class RealmConfiguration.Builder extends Object
RealmConfiguration.Builder used to construct instances of a RealmConfiguration in a fluent manner.
-
-
Constructor Summary
Constructors Constructor Description Builder()
Creates an instance of the Builder for the RealmConfiguration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RealmConfiguration.Builder
addModule(Object module)
FIXME: Temporary visible DEBUG method.RealmConfiguration.Builder
allowQueriesOnUiThread(boolean allowQueriesOnUiThread)
Sets whether or not aRealmQuery
can be launched from the UI thread.RealmConfiguration.Builder
allowWritesOnUiThread(boolean allowWritesOnUiThread)
Sets whether or not calls toRealm.executeTransaction(io.realm.Realm.Transaction)
are allowed from the UI thread.RealmConfiguration.Builder
assetFile(String assetFile)
Copies the Realm file from the given asset file path.RealmConfiguration
build()
Creates the RealmConfiguration based on the builder parameters.RealmConfiguration.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.RealmConfiguration.Builder
compactOnLaunch(CompactOnLaunchCallback compactOnLaunch)
Sets this to determine if the Realm file should be compacted before returned to the user.RealmConfiguration.Builder
deleteRealmIfMigrationNeeded()
Setting this will change the behavior of how migration exceptions are handled.RealmConfiguration.Builder
directory(File directory)
Specifies the directory where the Realm file will be saved.RealmConfiguration.Builder
encryptionKey(byte[] key)
Sets the 64 byte key used to encrypt and decrypt the Realm file.RealmConfiguration.Builder
flowFactory(FlowFactory factory)
Sets theFlowFactory
used to create coroutines Flows from Realm objects.RealmConfiguration.Builder
initialData(Realm.Transaction transaction)
Sets the initial data inRealm
.RealmConfiguration.Builder
inMemory()
Setting this will create an in-memory Realm instead of saving it to disk.RealmConfiguration.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.RealmConfiguration.Builder
migration(RealmMigration migration)
Sets theRealmMigration
to be run if a migration is needed.RealmConfiguration.Builder
modules(Object baseModule, Object... additionalModules)
Replaces the existing module(s) with one or moreRealmModule
s.RealmConfiguration.Builder
name(String filename)
Sets the filename for the Realm file.RealmConfiguration.Builder
readOnly()
Setting this will cause the Realm to become read only and all write transactions made against this Realm will fail with anIllegalStateException
.RealmConfiguration.Builder
rxFactory(RxObservableFactory factory)
Sets theRxObservableFactory
used to create Rx Observables from Realm objects.RealmConfiguration.Builder
schemaVersion(long schemaVersion)
Sets the schema version of the Realm.
-
-
-
Constructor Detail
-
Builder
public Builder()
Creates an instance of the Builder for the RealmConfiguration.This will use the app's own internal directory for storing the Realm file. This does not require any additional permissions. The default location is
/data/data/<packagename>/files
, but can change depending on vendor implementations of Android.
-
-
Method Detail
-
name
public RealmConfiguration.Builder name(String filename)
Sets the filename for the Realm file.
-
directory
public RealmConfiguration.Builder directory(File directory)
Specifies the directory where the Realm file will be saved. The default value iscontext.getFilesDir()
. If the directory does not exist, it will be created.- Parameters:
directory
- the directory to save the Realm file in. Directory must be writable.- Throws:
IllegalArgumentException
- ifdirectory
is null, not writable or a file.
-
encryptionKey
public RealmConfiguration.Builder encryptionKey(byte[] key)
Sets the 64 byte key used to encrypt and decrypt the Realm file. Sets the 64 bytes key used to encrypt and decrypt the Realm file.
-
schemaVersion
public RealmConfiguration.Builder schemaVersion(long schemaVersion)
Sets the schema version of the Realm. This must be equal to or higher than the schema version of the existing Realm file, if any. If the schema version is higher than the already existing Realm, a migration is needed.If no migration code is provided, Realm will throw a
RealmMigrationNeededException
.- See Also:
migration(RealmMigration)
-
migration
public RealmConfiguration.Builder migration(RealmMigration migration)
Sets theRealmMigration
to be run if a migration is needed. If this migration fails to upgrade the on-disc schema to the runtime schema, aRealmMigrationNeededException
will be thrown.
-
deleteRealmIfMigrationNeeded
public RealmConfiguration.Builder deleteRealmIfMigrationNeeded()
Setting this will change the behavior of how migration exceptions are handled. Instead of throwing aRealmMigrationNeededException
the on-disc Realm will be cleared and recreated with the new Realm schema.This cannot be configured to have an asset file at the same time by calling
assetFile(String)
as the provided asset file will be deleted in migrations.WARNING! This will result in loss of data.
- Throws:
IllegalStateException
- if configured to use an asset file by callingassetFile(String)
previously.
-
inMemory
public RealmConfiguration.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.
-
modules
public RealmConfiguration.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 doesn't have theRealmModule
annotation.- See Also:
Realm.getDefaultModule()
-
addModule
public final RealmConfiguration.Builder addModule(Object module)
FIXME: Temporary visible DEBUG method. Will add a module unconditionally. Adds a module to already defined modules.
-
rxFactory
public RealmConfiguration.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 RealmConfiguration.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 RealmConfiguration.Builder initialData(Realm.Transaction transaction)
Sets the initial data inRealm
. This transaction will be executed only for the first time when database file is created or while migrating the data whendeleteRealmIfMigrationNeeded()
is set.- Parameters:
transaction
- transaction to execute.
-
assetFile
public RealmConfiguration.Builder assetFile(String assetFile)
Copies the Realm file from the given asset file path.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 cannot be combined with
deleteRealmIfMigrationNeeded()
as doing so would just result in the copied file being deleted.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 is configured to clear its schema by callingdeleteRealmIfMigrationNeeded()
.
-
readOnly
public RealmConfiguration.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 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 RealmConfiguration.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 RealmConfiguration.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.
-
maxNumberOfActiveVersions
public RealmConfiguration.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 e.g. 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.
- Parameters:
number
- the maximum number of active versions before an exception is thrown.- See Also:
- FAQ
-
allowWritesOnUiThread
public RealmConfiguration.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 RealmConfiguration.Builder allowQueriesOnUiThread(boolean allowQueriesOnUiThread)
Sets whether or not aRealmQuery
can be launched 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 RealmConfiguration build()
Creates the RealmConfiguration based on the builder parameters.- Returns:
- the created
RealmConfiguration
.
-
-