public final class DynamicRealm
extends java.lang.Object
Realm
. This means that all access to data and/or queries are
done using string based class names instead of class type references.
This is useful during migrations or when working with string-based data like CSV or XML files.
The same RealmConfiguration
can be used to open a Realm file in both dynamic and typed mode, but
modifying the schema while having both a typed and dynamic version open is highly discouraged and will most likely
crash the typed Realm. During migrations only a DynamicRealm will be open.
Dynamic Realms do not enforce schemas or schema versions and RealmMigration
code is not used even if it has
been defined in the RealmConfiguration
.
This means that the schema is not created or validated until a Realm has been opened in typed mode, so if a Realm
file is opened in dynamic mode first it will not contain any information about classes and fields, and any queries
for classes defined by the schema will fail.Realm
,
RealmSchema
Modifier and Type | Class and Description |
---|---|
static interface |
DynamicRealm.Transaction
Encapsulates a Realm transaction.
|
Modifier and Type | Method and Description |
---|---|
void |
addChangeListener(RealmChangeListener<DynamicRealm> listener)
Adds a change listener to the Realm.
|
RealmResults<DynamicRealmObject> |
allObjects(java.lang.String className)
Deprecated.
|
RealmResults<DynamicRealmObject> |
allObjectsSorted(java.lang.String className,
java.lang.String[] fieldNames,
Sort[] sortOrders)
Deprecated.
|
RealmResults<DynamicRealmObject> |
allObjectsSorted(java.lang.String className,
java.lang.String fieldName,
Sort sortOrder)
Deprecated.
|
RealmResults<DynamicRealmObject> |
allObjectsSorted(java.lang.String className,
java.lang.String fieldName1,
Sort sortOrder1,
java.lang.String fieldName2,
Sort sortOrder2)
Deprecated.
|
<any> |
asObservable()
Returns an RxJava Observable that monitors changes to this Realm.
|
void |
beginTransaction()
Starts a transaction, this must be closed with
BaseRealm.commitTransaction() or aborted by
BaseRealm.cancelTransaction() . |
void |
cancelTransaction()
Reverts all writes (created, updated, or deleted objects) made in the current write transaction and end the
transaction.
|
void |
clear(java.lang.String className)
Deprecated.
|
void |
close()
Closes the Realm instance and all its resources.
|
void |
commitTransaction()
All changes since
BaseRealm.beginTransaction() are persisted to disk and the Realm reverts back to
being read-only. |
DynamicRealmObject |
createObject(java.lang.String className)
Instantiates and adds a new object to the Realm.
|
DynamicRealmObject |
createObject(java.lang.String className,
java.lang.Object primaryKeyValue)
Creates an object with a given primary key.
|
void |
delete(java.lang.String className)
Deletes all objects of the specified class from the Realm.
|
void |
deleteAll()
Deletes all objects from this Realm.
|
RealmResults<DynamicRealmObject> |
distinct(java.lang.String className,
java.lang.String fieldName)
Deprecated.
|
RealmResults<DynamicRealmObject> |
distinct(java.lang.String className,
java.lang.String firstFieldName,
java.lang.String... remainingFieldNames)
Deprecated.
|
RealmResults<DynamicRealmObject> |
distinctAsync(java.lang.String className,
java.lang.String fieldName)
Deprecated.
|
void |
executeTransaction(DynamicRealm.Transaction transaction)
Executes a given transaction on the DynamicRealm.
|
RealmConfiguration |
getConfiguration()
Returns the
RealmConfiguration for this Realm. |
static DynamicRealm |
getInstance(RealmConfiguration configuration)
Realm static constructor that returns a dynamic variant of the Realm instance defined by provided
RealmConfiguration . |
java.lang.String |
getPath()
Returns the canonical path to where this Realm is persisted on disk.
|
RealmSchema |
getSchema()
Returns the schema for this Realm.
|
long |
getVersion()
Returns the schema version for this Realm.
|
boolean |
isAutoRefresh()
Retrieves the auto-refresh status of the Realm instance.
|
boolean |
isClosed()
Checks if the
Realm instance has already been closed. |
boolean |
isEmpty()
Checks if this
Realm contains any objects. |
boolean |
isInTransaction()
Checks if the Realm is currently in a transaction.
|
void |
refresh()
Deprecated.
Please use
waitForChange() instead. |
void |
removeAllChangeListeners()
Removes all user-defined change listeners.
|
void |
removeChangeListener(RealmChangeListener<? extends io.realm.BaseRealm> listener)
Removes the specified change listener.
|
void |
setAutoRefresh(boolean autoRefresh)
Sets the auto-refresh status of the Realm instance.
|
void |
stopWaitForChange()
Makes any current
waitForChange() return false immediately. |
boolean |
waitForChange()
Blocks the current thread until new changes to the Realm are available or
stopWaitForChange()
is called from another thread. |
RealmQuery<DynamicRealmObject> |
where(java.lang.String className)
Returns a RealmQuery, which can be used to query for the provided class.
|
void |
writeCopyTo(java.io.File destination)
Writes a compacted copy of the Realm to the given destination File.
|
void |
writeEncryptedCopyTo(java.io.File destination,
byte[] key)
Writes a compacted and encrypted copy of the Realm to the given destination File.
|
public static DynamicRealm getInstance(RealmConfiguration configuration)
RealmConfiguration
. Dynamic Realms do not care about schemaVersion and schemas, so opening a
DynamicRealm will never trigger a migration.RealmIOException
- if an error happened when accessing the underlying Realm file.java.lang.IllegalArgumentException
- if configuration
argument is null
.for details on how to configure a Realm.
public DynamicRealmObject createObject(java.lang.String className)
className
- the class name of the object to create.RealmException
- if the object could not be created.public DynamicRealmObject createObject(java.lang.String className, java.lang.Object primaryKeyValue)
createObject(String)
} instead.RealmException
- if object could not be created due to the primary key being invalid.java.lang.IllegalStateException
- If the model clazz does not have an primary key defined.java.lang.IllegalArgumentException
- if the primaryKeyValue
doesn't have a value that can be converted to the
expectd value.public RealmQuery<DynamicRealmObject> where(java.lang.String className)
className
- The class of the object which is to be queried for.java.lang.IllegalArgumentException
- if the class doesn't exist.RealmQuery
public void addChangeListener(RealmChangeListener<DynamicRealm> listener)
The listeners will be executed:
BaseRealm.refresh()
removeChangeListener(RealmChangeListener)
or removeAllChangeListeners()
which removes all listeners including the ones added via anonymous classes.listener
- the change listener.java.lang.IllegalStateException
- if you try to register a listener from a non-Looper Thread.RealmChangeListener
,
removeChangeListener(RealmChangeListener)
,
removeAllChangeListeners()
@Deprecated public void clear(java.lang.String className)
delete(String)
instead.className
- the class for which all objects should be removed.public void delete(java.lang.String className)
className
- the class for which all objects should be removed.public void executeTransaction(DynamicRealm.Transaction transaction)
beginTransaction()
and
commitTransaction()
will be called automatically. If any exception is thrown
during the transaction cancelTransaction()
will be called instead of commitTransaction()
.transaction
- DynamicRealm.Transaction
to execute.java.lang.IllegalArgumentException
- if the transaction
is null
.@Deprecated public RealmResults<DynamicRealmObject> allObjects(java.lang.String className)
dynamicRealm.where(className).findAll()
instead.@Deprecated public RealmResults<DynamicRealmObject> allObjectsSorted(java.lang.String className, java.lang.String fieldName, Sort sortOrder)
dynamicRealm.where(className).findAll(fieldName, sortOrder)
instead.@Deprecated public RealmResults<DynamicRealmObject> allObjectsSorted(java.lang.String className, java.lang.String fieldName1, Sort sortOrder1, java.lang.String fieldName2, Sort sortOrder2)
dynamicRealm.where(className).findAll(fieldName1, sortOrder1, fieldName2, sortOrder2)
instead.@Deprecated public RealmResults<DynamicRealmObject> allObjectsSorted(java.lang.String className, java.lang.String[] fieldNames, Sort[] sortOrders)
dynamicRealm.where(className).findAll(fieldNames[], sortOrders[])
instead.@Deprecated public RealmResults<DynamicRealmObject> distinct(java.lang.String className, java.lang.String fieldName)
dynamicRealm.where(className).distinct(fieldName)
instead.@Deprecated public RealmResults<DynamicRealmObject> distinctAsync(java.lang.String className, java.lang.String fieldName)
dynamicRealm.where(className).distinctAsync(fieldName)
instead.@Deprecated public RealmResults<DynamicRealmObject> distinct(java.lang.String className, java.lang.String firstFieldName, java.lang.String... remainingFieldNames)
dynamicRealm.where(className).distinct(firstFieldName, remainingFieldNames)
instead.public <any> asObservable()
onComplete
will never be called.
If you would like the asObservable()
to stop emitting items you can instruct RxJava to
only emit only the first item by using the first()
operator:
realm.asObservable().first().subscribe( ... ) // You only get the results once
onNext
. It will never call onComplete
or OnError
.public void setAutoRefresh(boolean autoRefresh)
Auto-refresh is a feature that enables automatic update of the current Realm instance and all its derived objects
(RealmResults and RealmObjects instances) when a commit is performed on a Realm acting on the same file in
another thread. This feature is only available if the Realm instance lives is a Looper
enabled
thread.
autoRefresh
- true
will turn auto-refresh on, false
will turn it off.java.lang.IllegalStateException
- if called from a non-Looper thread.public boolean isAutoRefresh()
public boolean isInTransaction()
true
if inside a transaction, false
otherwise.public void removeChangeListener(RealmChangeListener<? extends io.realm.BaseRealm> listener)
listener
- the change listener to be removed.java.lang.IllegalStateException
- if you try to remove a listener from a non-Looper Thread.RealmChangeListener
public void removeAllChangeListeners()
java.lang.IllegalStateException
- if you try to remove listeners from a non-Looper Thread.RealmChangeListener
public void writeCopyTo(java.io.File destination) throws java.io.IOException
The destination file cannot already exist.
Note that if this is called from within a transaction it writes the current data, and not the data as it was when the last transaction was committed.
destination
- file to save the Realm to.java.io.IOException
- if any write operation fails.public void writeEncryptedCopyTo(java.io.File destination, byte[] key) throws java.io.IOException
The destination file cannot already exist.
Note that if this is called from within a transaction it writes the current data, and not the data as it was when the last transaction was committed.
destination
- file to save the Realm to.key
- a 64-byte encryption key.java.io.IOException
- if any write operation fails.java.lang.IllegalArgumentException
- if destination argument is null.@Deprecated public void refresh()
waitForChange()
instead.java.lang.IllegalStateException
- if attempting to refresh from within a transaction.public boolean waitForChange()
stopWaitForChange()
is called from another thread. Once stopWaitForChange is called, all future calls to this method will
return false immediately.true
if the Realm was updated to the latest version, false
if it was
cancelled by calling stopWaitForChange.java.lang.IllegalStateException
- if calling this from within a transaction or from a Looper thread.public void stopWaitForChange()
waitForChange()
return false
immediately. Once this is called,
all future calls to waitForChange will immediately return false
.
This method is thread-safe and should _only_ be called from another thread than the one that called waitForChange.
java.lang.IllegalStateException
- if the Realm
instance has already been closed.public void beginTransaction()
BaseRealm.commitTransaction()
or aborted by
BaseRealm.cancelTransaction()
. Transactions are used to atomically create, update and delete objects
within a Realm.
BaseRealm.beginTransaction()
updates the realm in the case of
pending updates from other threads.
public void commitTransaction()
BaseRealm.beginTransaction()
are persisted to disk and the Realm reverts back to
being read-only. An event is sent to notify all other Realm instances that a change has occurred. When the event
is received, the other Realms will get their objects and RealmResults
updated to reflect the
changes from this commit.public void cancelTransaction()
public java.lang.String getPath()
File.getCanonicalPath()
public RealmConfiguration getConfiguration()
RealmConfiguration
for this Realm.RealmConfiguration
for this Realm.public long getVersion()
public void close()
It's important to always remember to close Realm instances when you're done with it in order not to leak memory, file descriptors or grow the size of Realm file out of measure.
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.lang.IllegalStateException
- if attempting to close from another thread.public boolean isClosed()
Realm
instance has already been closed.true
if closed, false
otherwise.java.lang.IllegalStateException
- if attempting to close from another thread.public boolean isEmpty()
Realm
contains any objects.true
if empty, @{code false} otherwise.public RealmSchema getSchema()
RealmSchema
for this Realm.public void deleteAll()
java.lang.IllegalStateException
- if the corresponding Realm is closed or called from an incorrect thread.