public static class RealmConfiguration.Builder extends Object
Constructor and Description |
---|
Builder()
Creates an instance of the Builder for the RealmConfiguration.
|
Modifier and Type | Method and Description |
---|---|
RealmConfiguration.Builder |
addModule(Object module)
FIXME: Temporary visible
DEBUG method.
|
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 |
initialData(Realm.Transaction transaction)
Sets the initial data in
Realm . |
RealmConfiguration.Builder |
inMemory()
Setting this will create an in-memory Realm instead of saving it to disk.
|
RealmConfiguration.Builder |
migration(RealmMigration migration)
Sets the
RealmMigration to be run if a migration is needed. |
RealmConfiguration.Builder |
modules(Object baseModule,
Object... additionalModules)
Replaces the existing module(s) with one or more
RealmModule 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 an
IllegalStateException . |
RealmConfiguration.Builder |
rxFactory(RxObservableFactory factory)
Sets the
RxObservableFactory used to create Rx Observables from Realm objects. |
RealmConfiguration.Builder |
schemaVersion(long schemaVersion)
Sets the schema version of the Realm.
|
public Builder()
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.
public RealmConfiguration.Builder name(String filename)
public RealmConfiguration.Builder directory(File directory)
context.getFilesDir()
.
If the directory does not exist, it will be created.directory
- the directory to save the Realm file in. Directory must be writable.IllegalArgumentException
- if directory
is null, not writable or a file.public RealmConfiguration.Builder encryptionKey(byte[] key)
public RealmConfiguration.Builder schemaVersion(long schemaVersion)
If no migration code is provided, Realm will throw a
RealmMigrationNeededException
.
migration(RealmMigration)
public RealmConfiguration.Builder migration(RealmMigration migration)
RealmMigration
to be run if a migration is needed. If this migration fails to
upgrade the on-disc schema to the runtime schema, a RealmMigrationNeededException
will be thrown.public RealmConfiguration.Builder deleteRealmIfMigrationNeeded()
RealmMigrationNeededException
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.
IllegalStateException
- if configured to use an asset file by calling assetFile(String)
previously.public RealmConfiguration.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 RealmConfiguration.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 doesn't have the RealmModule
annotation.Realm.getDefaultModule()
public final RealmConfiguration.Builder addModule(Object module)
public RealmConfiguration.Builder rxFactory(RxObservableFactory factory)
RxObservableFactory
used to create Rx Observables from Realm objects.
The default factory is RealmObservableFactory
.factory
- factory to use.public RealmConfiguration.Builder initialData(Realm.Transaction transaction)
Realm
. This transaction will be executed only for the first time
when database file is created or while migrating the data when deleteRealmIfMigrationNeeded()
is set.transaction
- transaction to execute.public RealmConfiguration.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 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.
assetFile
- path to the asset database file.IllegalStateException
- if this is configured to clear its schema by calling deleteRealmIfMigrationNeeded()
.public RealmConfiguration.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 RealmConfiguration.Builder compactOnLaunch()
DefaultCompactOnLaunchCallback
for details.public RealmConfiguration.Builder compactOnLaunch(CompactOnLaunchCallback compactOnLaunch)
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.public RealmConfiguration build()
RealmConfiguration
.