public final class DynamicRealm extends BaseRealm
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 |
---|---|
RealmResults<DynamicRealmObject> |
allObjects(java.lang.String className)
Gets all objects of a specific class name.
|
RealmResults<DynamicRealmObject> |
allObjectsSorted(java.lang.String className,
java.lang.String[] fieldNames,
Sort[] sortOrders)
Gets all objects of a specific class name sorted by multiple fields.
|
RealmResults<DynamicRealmObject> |
allObjectsSorted(java.lang.String className,
java.lang.String fieldName,
Sort sortOrder)
Gets 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)
Gets all objects of a specific class name sorted by two specific field names.
|
<any> |
asObservable()
Returns an RxJava Observable that monitors changes to this Realm.
|
void |
clear(java.lang.String className)
Deprecated.
|
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.
|
RealmResults<DynamicRealmObject> |
distinct(java.lang.String className,
java.lang.String fieldName)
Returns a distinct set of objects of a specific class.
|
RealmResults<DynamicRealmObject> |
distinct(java.lang.String className,
java.lang.String firstFieldName,
java.lang.String... remainingFieldNames)
Returns a distinct set of objects from a specific class.
|
RealmResults<DynamicRealmObject> |
distinctAsync(java.lang.String className,
java.lang.String fieldName)
Returns a distinct set of objects of a specific class.
|
void |
executeTransaction(DynamicRealm.Transaction transaction)
Executes a given transaction on the DynamicRealm.
|
static DynamicRealm |
getInstance(RealmConfiguration configuration)
Realm static constructor that returns a dynamic variant of the Realm instance defined by provided
RealmConfiguration . |
RealmQuery<DynamicRealmObject> |
where(java.lang.String className)
Returns a RealmQuery, which can be used to query for the provided class.
|
addChangeListener, beginTransaction, cancelTransaction, close, commitTransaction, deleteAll, getConfiguration, getPath, getSchema, getVersion, isAutoRefresh, isClosed, isEmpty, isInTransaction, refresh, removeAllChangeListeners, removeChangeListener, setAutoRefresh, writeCopyTo, writeEncryptedCopyTo
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.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
@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)
BaseRealm.beginTransaction()
and
BaseRealm.commitTransaction()
will be called automatically. If any exception is thrown
during the transaction BaseRealm.cancelTransaction()
will be called instead of BaseRealm.commitTransaction()
.transaction
- DynamicRealm.Transaction
to execute.java.lang.IllegalArgumentException
- if the transaction
is null
.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.ASCENDING, sort descending if Sort.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 is null, does not exist, is an unsupported type,
is not indexed, or points to linked fields.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 is null, does not exist, is an unsupported type,
is not indexed, or points to linked fields.public RealmResults<DynamicRealmObject> distinct(java.lang.String className, java.lang.String firstFieldName, java.lang.String... remainingFieldNames)
className
- the Class to get objects of.firstFieldName
- first field name to use when finding distinct objects.remainingFieldNames
- remaining field names when determining all unique combinations of field values.RealmResults
containing the distinct objects.java.lang.IllegalArgumentException
- if field names is empty or null
, does not exist,
is an unsupported type, or points to a linked field.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
asObservable
in class BaseRealm
onNext
. It will never call onComplete
or OnError
.