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 listener)
Adds a change listener to the Realm.
|
RealmResults<DynamicRealmObject> |
allObjects(java.lang.String className)
Get all objects of a specific class name.
|
RealmResults<DynamicRealmObject> |
allObjectsSorted(java.lang.String className,
java.lang.String[] fieldNames,
Sort[] sortOrders)
Get all objects of a specific class name sorted by multiple fields.
|
RealmResults<DynamicRealmObject> |
allObjectsSorted(java.lang.String className,
java.lang.String fieldName,
Sort sortOrder)
Get all objects of a specific class name sorted by a field.
|
RealmResults<DynamicRealmObject> |
allObjectsSorted(java.lang.String className,
java.lang.String fieldName1,
Sort sortOrder1,
java.lang.String fieldName2,
Sort sortOrder2)
Get all objects of a specific class name sorted by two specific field names.
|
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)
Removes all objects of the specified class.
|
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. |
static boolean |
compactRealm(RealmConfiguration configuration)
Compacts the Realm file defined by the given configuration.
|
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.
|
RealmResults<DynamicRealmObject> |
distinct(java.lang.String className,
java.lang.String fieldName)
Return a distinct set of objects of a specific class.
|
RealmResults<DynamicRealmObject> |
distinctAsync(java.lang.String className,
java.lang.String fieldName)
Return a distinct set of objects of a specific class.
|
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()
Refreshes the Realm instance and all the RealmResults and RealmObjects instances coming from it.
|
void |
removeAllChangeListeners()
Removes all user-defined change listeners.
|
void |
removeChangeListener(RealmChangeListener listener)
Removes the specified change listener.
|
void |
setAutoRefresh(boolean autoRefresh)
Sets the auto-refresh status of the Realm instance.
|
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.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.java.lang.IllegalArgumentException
- if the primary key value is of the wrong type.java.lang.IllegalStateException
- if the class doesn't have a primary key defined.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 clear(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.RealmException
- if any error happened during the transaction.public RealmResults<DynamicRealmObject> allObjects(java.lang.String className)
className
- the Class to get objects of.RealmResults
list containing the objects. If no results where found, an empty list
will be returned.RealmResults
public RealmResults<DynamicRealmObject> allObjectsSorted(java.lang.String className, java.lang.String fieldName, Sort sortOrder)
RealmResults
will not be null
. Use RealmResults.size()
to check the number of objects instead.className
- the class to get all objects from.fieldName
- the field name to sort by.sortOrder
- how to sort the results.RealmResults
containing the objects.java.lang.IllegalArgumentException
- if field name does not exist.public RealmResults<DynamicRealmObject> allObjectsSorted(java.lang.String className, java.lang.String fieldName1, Sort sortOrder1, java.lang.String fieldName2, Sort sortOrder2)
RealmResults
will not be null
. Use RealmResults.size()
to check the number of
objects instead.className
- the class to get all objects from.fieldName1
- the first field name to sort by.sortOrder1
- how to sort the first field.fieldName2
- the second field name to sort by.sortOrder2
- how to sort the second field.RealmResults
containing the objects. If no results where found an empty list
is returned.java.lang.IllegalArgumentException
- if a field name used for sorting does not exist.public RealmResults<DynamicRealmObject> allObjectsSorted(java.lang.String className, java.lang.String[] fieldNames, Sort[] sortOrders)
RealmResults
will not be null
. Use RealmResults.size()
to check the number of
objects instead.className
- the class to get all objects from.sortOrders
- sort ascending if SORT_ORDER_ASCENDING, sort descending if SORT_ORDER_DESCENDING.fieldNames
- an array of field names to sort objects by.
The objects are first sorted by fieldNames[0], then by fieldNames[1] and so forth.RealmResults
containing the objects.java.lang.IllegalArgumentException
- if a field name does not exist.public RealmResults<DynamicRealmObject> distinct(java.lang.String className, java.lang.String fieldName)
className
- the Class to get objects of.fieldName
- the field name.RealmResults
containing the distinct objects.java.lang.IllegalArgumentException
- if a field name does not exist or the field is not indexed.public RealmResults<DynamicRealmObject> distinctAsync(java.lang.String className, java.lang.String fieldName)
className
- the Class to get objects of.fieldName
- the field name.RealmResults
. Users need to register a listener
RealmResults.addChangeListener(RealmChangeListener)
to be notified
when the query completes.java.lang.IllegalArgumentException
- if a field name does not exist or the field is not indexed.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.public boolean isAutoRefresh()
public boolean isInTransaction()
true
if inside a transaction, false
otherwise.public void addChangeListener(RealmChangeListener 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.RealmChangeListener
,
removeChangeListener(RealmChangeListener)
,
removeAllChangeListeners()
public void removeChangeListener(RealmChangeListener listener)
listener
- the change listener to be removed.RealmChangeListener
,
addChangeListener(RealmChangeListener)
public void removeAllChangeListeners()
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.public void refresh()
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
public boolean isClosed()
Realm
instance has already been closed.true
if closed, false
otherwise.public boolean isEmpty()
Realm
contains any objects.true
if empty, @{code false} otherwise.public RealmSchema getSchema()
RealmSchema
for this Realm.public static boolean compactRealm(RealmConfiguration configuration)