public final class Realm
extends java.lang.Object
beginTransaction()
The transactions ensure that multiple instances (on multiple threads) can access the same objects in a consistent state with full ACID guarantees.
It is important to remember to call the close()
method when done with a Realm instance. Failing to do so can
lead to OutOfMemoryError
as the native resources cannot be freed.
Realm instances cannot be used across different threads. This means that you have to open an instance on each thread
you want to use Realm. Realm instances are cached automatically per thread using reference counting, so as long as
the reference count doesn't reach zero, calling getInstance(RealmConfiguration)
will just return the cached
Realm and should be considered a lightweight operation.
For the UI thread this means that opening and closing Realms should occur in either onCreate/onDestroy or onStart/onStop.
Realm instances coordinate their state across threads using the Handler
mechanism. This also means
that Realm instances on threads without a Looper
cannot receive updates unless refresh()
is manually called.
A standard pattern for working with Realm in Android activities can be seen below:
public class RealmApplication extends Application { \@Override public void onCreate() { super.onCreate(); // The Realm file will be located in package's "files" directory. RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build(); Realm.setDefaultConfiguration(realmConfig); } } public class RealmActivity extends Activity { private Realm realm; \@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main); realm = Realm.getDefaultInstance(); } \@Override protected void onDestroy() { super.onDestroy(); realm.close(); } }
Realm supports String and byte fields containing up to 16 MB.
Modifier and Type | Class and Description |
---|---|
static interface |
Realm.Transaction
Encapsulates a Realm transaction.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_REALM_NAME |
Modifier and Type | Method and Description |
---|---|
void |
addChangeListener(RealmChangeListener<Realm> listener)
Adds a change listener to the Realm.
|
<E extends RealmModel> |
allObjects(java.lang.Class<E> clazz)
Deprecated.
|
<E extends RealmModel> |
allObjectsSorted(java.lang.Class<E> clazz,
java.lang.String[] fieldNames,
Sort[] sortOrders)
Deprecated.
|
<E extends RealmModel> |
allObjectsSorted(java.lang.Class<E> clazz,
java.lang.String fieldName,
Sort sortOrder)
Deprecated.
|
<E extends RealmModel> |
allObjectsSorted(java.lang.Class<E> clazz,
java.lang.String fieldName1,
Sort sortOrder1,
java.lang.String fieldName2,
Sort sortOrder2)
Deprecated.
|
<E extends RealmModel> |
allObjectsSorted(java.lang.Class<E> clazz,
java.lang.String fieldName1,
Sort sortOrder1,
java.lang.String fieldName2,
Sort sortOrder2,
java.lang.String fieldName3,
Sort sortOrder3)
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.Class<? extends RealmModel> clazz)
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. |
static boolean |
compactRealm(RealmConfiguration configuration)
Compacts a Realm file.
|
<E extends RealmModel> |
copyFromRealm(E realmObject)
Makes a standalone in-memory copy of an already persisted
RealmObject . |
<E extends RealmModel> |
copyFromRealm(E realmObject,
int maxDepth)
Makes a standalone in-memory copy of an already persisted
RealmObject . |
<E extends RealmModel> |
copyFromRealm(java.lang.Iterable<E> realmObjects)
Makes a standalone in-memory copy of already persisted RealmObjects.
|
<E extends RealmModel> |
copyFromRealm(java.lang.Iterable<E> realmObjects,
int maxDepth)
Makes a standalone in-memory copy of already persisted RealmObjects.
|
<E extends RealmModel> |
copyToRealm(E object)
Copies a RealmObject to the Realm instance and returns the copy.
|
<E extends RealmModel> |
copyToRealm(java.lang.Iterable<E> objects)
Copies a collection of RealmObjects to the Realm instance and returns their copy.
|
<E extends RealmModel> |
copyToRealmOrUpdate(E object)
Updates an existing RealmObject that is identified by the same
PrimaryKey or creates
a new copy if no existing object could be found. |
<E extends RealmModel> |
copyToRealmOrUpdate(java.lang.Iterable<E> objects)
Updates a list of existing RealmObjects that is identified by their
PrimaryKey or
creates a new copy if no existing object could be found. |
<E extends RealmModel> |
createAllFromJson(java.lang.Class<E> clazz,
java.io.InputStream inputStream)
Creates a Realm object for each object in a JSON array.
|
<E extends RealmModel> |
createAllFromJson(java.lang.Class<E> clazz,
org.json.JSONArray json)
Creates a Realm object for each object in a JSON array.
|
<E extends RealmModel> |
createAllFromJson(java.lang.Class<E> clazz,
java.lang.String json)
Creates a Realm object for each object in a JSON array.
|
<E extends RealmModel> |
createObject(java.lang.Class<E> clazz)
Instantiates and adds a new object to the Realm.
|
<E extends RealmModel> |
createObject(java.lang.Class<E> clazz,
java.lang.Object primaryKeyValue)
Instantiates and adds a new object to the Realm with the primary key value already set.
|
<E extends RealmModel> |
createObjectFromJson(java.lang.Class<E> clazz,
java.io.InputStream inputStream)
Creates a Realm object pre-filled with data from a JSON object.
|
<E extends RealmModel> |
createObjectFromJson(java.lang.Class<E> clazz,
org.json.JSONObject json)
Creates a Realm object pre-filled with data from a JSON object.
|
<E extends RealmModel> |
createObjectFromJson(java.lang.Class<E> clazz,
java.lang.String json)
Creates a Realm object pre-filled with data from a JSON object.
|
<E extends RealmModel> |
createOrUpdateAllFromJson(java.lang.Class<E> clazz,
java.io.InputStream in)
Tries to update a list of existing objects identified by their primary key with new JSON data.
|
<E extends RealmModel> |
createOrUpdateAllFromJson(java.lang.Class<E> clazz,
org.json.JSONArray json)
Tries to update a list of existing objects identified by their primary key with new JSON data.
|
<E extends RealmModel> |
createOrUpdateAllFromJson(java.lang.Class<E> clazz,
java.lang.String json)
Tries to update a list of existing objects identified by their primary key with new JSON data.
|
<E extends RealmModel> |
createOrUpdateObjectFromJson(java.lang.Class<E> clazz,
java.io.InputStream in)
Tries to update an existing object defined by its primary key with new JSON data.
|
<E extends RealmModel> |
createOrUpdateObjectFromJson(java.lang.Class<E> clazz,
org.json.JSONObject json)
Tries to update an existing object defined by its primary key with new JSON data.
|
<E extends RealmModel> |
createOrUpdateObjectFromJson(java.lang.Class<E> clazz,
java.lang.String json)
Tries to update an existing object defined by its primary key with new JSON data.
|
void |
delete(java.lang.Class<? extends RealmModel> clazz)
Deletes all objects of the specified class from the Realm.
|
void |
deleteAll()
Deletes all objects from this Realm.
|
static boolean |
deleteRealm(RealmConfiguration configuration)
Deletes the Realm file specified by the given
RealmConfiguration from the filesystem. |
<E extends RealmModel> |
distinct(java.lang.Class<E> clazz,
java.lang.String fieldName)
Deprecated.
|
<E extends RealmObject> |
distinct(java.lang.Class<E> clazz,
java.lang.String firstFieldName,
java.lang.String... remainingFieldNames)
Deprecated.
|
<E extends RealmModel> |
distinctAsync(java.lang.Class<E> clazz,
java.lang.String fieldName)
Deprecated.
|
void |
executeTransaction(Realm.Transaction transaction)
Executes a given transaction on the Realm.
|
RealmAsyncTask |
executeTransaction(Realm.Transaction transaction,
Realm.Transaction.Callback callback)
|
RealmAsyncTask |
executeTransactionAsync(Realm.Transaction transaction)
Similar to
executeTransaction(Transaction) but runs asynchronously on a worker thread. |
RealmAsyncTask |
executeTransactionAsync(Realm.Transaction transaction,
Realm.Transaction.OnError onError)
Similar to
executeTransactionAsync(Transaction) , but also accepts an OnError callback. |
RealmAsyncTask |
executeTransactionAsync(Realm.Transaction transaction,
Realm.Transaction.OnSuccess onSuccess)
Similar to
executeTransactionAsync(Transaction) , but also accepts an OnSuccess callback. |
RealmAsyncTask |
executeTransactionAsync(Realm.Transaction transaction,
Realm.Transaction.OnSuccess onSuccess,
Realm.Transaction.OnError onError)
Similar to
executeTransactionAsync(Transaction) , but also accepts an OnSuccess and OnError callbacks. |
RealmConfiguration |
getConfiguration()
Returns the
RealmConfiguration for this Realm. |
static Realm |
getDefaultInstance()
Realm static constructor that returns the Realm instance defined by the
RealmConfiguration set
by setDefaultConfiguration(RealmConfiguration) |
static java.lang.Object |
getDefaultModule()
Returns the default Realm module.
|
static Realm |
getInstance(android.content.Context context)
Deprecated.
use
getDefaultInstance() or getInstance(RealmConfiguration) instead. |
static Realm |
getInstance(RealmConfiguration configuration)
Realm static constructor that returns 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.
|
static void |
migrateRealm(RealmConfiguration configuration)
Manually triggers the migration associated with a given RealmConfiguration.
|
static void |
migrateRealm(RealmConfiguration configuration,
RealmMigration migration)
Manually triggers a migration on a RealmMigration.
|
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.
|
static void |
removeDefaultConfiguration()
Removes the current default configuration (if any).
|
void |
setAutoRefresh(boolean autoRefresh)
Sets the auto-refresh status of the Realm instance.
|
static void |
setDefaultConfiguration(RealmConfiguration configuration)
Sets the
RealmConfiguration used when calling getDefaultInstance() . |
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. |
<E extends RealmModel> |
where(java.lang.Class<E> clazz)
Returns a typed RealmQuery, which can be used to query for specific objects of this type
|
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 final java.lang.String DEFAULT_REALM_NAME
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 static Realm getInstance(android.content.Context context)
getDefaultInstance()
or getInstance(RealmConfiguration)
instead.Realm.getInstance(new RealmConfiguration(getContext()).build())
.
This constructor is only provided for convenience. It is recommended to use
getInstance(RealmConfiguration)
or getDefaultInstance()
.context
- a non-null Android Context
java.lang.IllegalArgumentException
- if no Context
is provided.RealmMigrationNeededException
- if the RealmObject classes no longer match the underlying Realm and it must
be migrated.RealmIOException
- if an error happened when accessing the underlying Realm file.public static Realm getDefaultInstance()
RealmConfiguration
set
by setDefaultConfiguration(RealmConfiguration)
java.lang.NullPointerException
- if no default configuration has been defined.RealmMigrationNeededException
- if no migration has been provided by the default configuration and the
RealmObject classes or version has has changed so a migration is required.RealmIOException
- if an error happened when accessing the underlying Realm file.public static Realm getInstance(RealmConfiguration configuration)
RealmConfiguration
configuration
- RealmConfiguration
used to open the RealmRealmMigrationNeededException
- if no migration has been provided by the configuration and the RealmObject
classes or version has has changed so a migration is required.RealmIOException
- if an error happened when accessing the underlying Realm file.java.lang.IllegalArgumentException
- if a null RealmConfiguration
is provided.for details on how to configure a Realm.
public static void setDefaultConfiguration(RealmConfiguration configuration)
RealmConfiguration
used when calling getDefaultInstance()
.configuration
- the RealmConfiguration
to use as the default configuration.java.lang.IllegalArgumentException
- if a null RealmConfiguration
is provided.for details on how to configure a Realm.
public static void removeDefaultConfiguration()
getDefaultInstance()
will
fail until a new default configuration has been set using setDefaultConfiguration(RealmConfiguration)
.public <E extends RealmModel> void createAllFromJson(java.lang.Class<E> clazz, org.json.JSONArray json)
RealmObject
field is not present in the JSON object the RealmObject
field will be set to the default value for that type.clazz
- type of Realm objects to create.json
- an array where each JSONObject must map to the specified class.RealmException
- if mapping from JSON fails.public <E extends RealmModel> void createOrUpdateAllFromJson(java.lang.Class<E> clazz, org.json.JSONArray json)
RealmObject
and a field is not found in the JSON object, that field will not be updated. If
a new RealmObject
is created and a field is not found in the JSON object, that field will be assigned the
default value for the field type.clazz
- type of RealmObject
to create or update. It must have a primary key defined.json
- array with object data.java.lang.IllegalArgumentException
- if trying to update a class without a
PrimaryKey
.RealmException
- if unable to map JSON.createAllFromJson(Class, org.json.JSONArray)
public <E extends RealmModel> void createAllFromJson(java.lang.Class<E> clazz, java.lang.String json)
RealmObject
field is not present in the JSON object the RealmObject
field
will be set to the default value for that type.clazz
- type of Realm objects to create.json
- the JSON array as a String where each object can map to the specified class.RealmException
- if mapping from JSON fails.public <E extends RealmModel> void createOrUpdateAllFromJson(java.lang.Class<E> clazz, java.lang.String json)
RealmObject
and a field is not found in the JSON object, that field will not be updated.
If a new RealmObject
is created and a field is not found in the JSON object, that field will be assigned
the default value for the field type.clazz
- type of RealmObject
to create or update. It must have a primary key defined.json
- string with an array of JSON objects.java.lang.IllegalArgumentException
- if trying to update a class without a
PrimaryKey
.RealmException
- if unable to create a JSON array from the json string.createAllFromJson(Class, String)
public <E extends RealmModel> void createAllFromJson(java.lang.Class<E> clazz, java.io.InputStream inputStream) throws java.io.IOException
RealmObject
field is not present in the JSON object the RealmObject
field
will be set to the default value for that type.clazz
- type of Realm objects created.inputStream
- the JSON array as a InputStream. All objects in the array must be of the specified class.RealmException
- if mapping from JSON fails.java.io.IOException
- if something was wrong with the input stream.public <E extends RealmModel> void createOrUpdateAllFromJson(java.lang.Class<E> clazz, java.io.InputStream in) throws java.io.IOException
RealmObject
and a field is not found in the JSON object, that field will not be updated.
If a new RealmObject
is created and a field is not found in the JSON object, that field will be assigned
the default value for the field type.clazz
- type of RealmObject
to create or update. It must have a primary key defined.in
- the InputStream with a list of object data in JSON format.java.lang.IllegalArgumentException
- if trying to update a class without a
PrimaryKey
.RealmException
- if unable to read JSON.java.io.IOException
createOrUpdateAllFromJson(Class, java.io.InputStream)
public <E extends RealmModel> E createObjectFromJson(java.lang.Class<E> clazz, org.json.JSONObject json)
RealmObject
field is not present in the JSON object the RealmObject
field will
be set to the default value for that type.clazz
- type of Realm object to create.json
- the JSONObject with object data.RealmException
- if the mapping from JSON fails.createOrUpdateObjectFromJson(Class, org.json.JSONObject)
public <E extends RealmModel> E createOrUpdateObjectFromJson(java.lang.Class<E> clazz, org.json.JSONObject json)
RealmObject
and a field is not found in the JSON object, that field will not be updated. If a new RealmObject
is
created and a field is not found in the JSON object, that field will be assigned the default value for the field type.clazz
- Type of RealmObject
to create or update. It must have a primary key defined.json
- JSONObject
with object data.RealmObject
.java.lang.IllegalArgumentException
- if trying to update a class without a
PrimaryKey
.RealmException
- if JSON data cannot be mapped.createObjectFromJson(Class, org.json.JSONObject)
public <E extends RealmModel> E createObjectFromJson(java.lang.Class<E> clazz, java.lang.String json)
RealmObject
field is not present in the JSON object the RealmObject
field will
be set to the default value for that type.clazz
- type of Realm object to create.json
- the JSON string with object data.RealmException
- if mapping to json failed.public <E extends RealmModel> E createOrUpdateObjectFromJson(java.lang.Class<E> clazz, java.lang.String json)
RealmObject
and a field is not found in the JSON object, that field will not be updated. If a new
RealmObject
is created and a field is not found in the JSON object, that field will be assigned the
default value for the field type.clazz
- type of RealmObject
to create or update. It must have a primary key defined.json
- string with object data in JSON format.RealmObject
.java.lang.IllegalArgumentException
- if trying to update a class without a
PrimaryKey
.RealmException
- if JSON object cannot be mapped from the string parameter.createObjectFromJson(Class, String)
public <E extends RealmModel> E createObjectFromJson(java.lang.Class<E> clazz, java.io.InputStream inputStream) throws java.io.IOException
RealmObject
field is not present in the JSON object the RealmObject
field will
be set to the default value for that type.clazz
- type of Realm object to create.inputStream
- the JSON object data as a InputStream.RealmException
- if the mapping from JSON failed.java.io.IOException
- if something was wrong with the input stream.public <E extends RealmModel> E createOrUpdateObjectFromJson(java.lang.Class<E> clazz, java.io.InputStream in) throws java.io.IOException
RealmObject
and a field is not found in the JSON object, that field will not be updated. If a new
RealmObject
is created and a field is not found in the JSON object, that field will be assigned the
default value for the field type.clazz
- type of RealmObject
to create or update. It must have a primary key defined.in
- the InputStream
with object data in JSON format.RealmObject
.java.lang.IllegalArgumentException
- if trying to update a class without a
PrimaryKey
.RealmException
- if failure to read JSON.java.io.IOException
createObjectFromJson(Class, java.io.InputStream)
public <E extends RealmModel> E createObject(java.lang.Class<E> clazz)
clazz
- the Class of the object to createRealmException
- if an object could not be createdpublic <E extends RealmModel> E createObject(java.lang.Class<E> clazz, java.lang.Object primaryKeyValue)
RealmException
will be
thrown.clazz
- the Class of the object to create.primaryKeyValue
- value for the primary key field.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
expected value.public <E extends RealmModel> E copyToRealm(E object)
object
- the RealmObject
to copy to the Realm.java.lang.IllegalArgumentException
- if the object is null
or it belongs to a Realm instance
in a different thread.public <E extends RealmModel> E copyToRealmOrUpdate(E object)
PrimaryKey
or creates
a new copy if no existing object could be found. This is a deep copy or update, so all referenced objects will be
either copied or updated.
Please note, copying an object will copy all field values. All unset fields in this and child objects will be
set to their default value if not provided.object
- RealmObject
to copy or update.java.lang.IllegalArgumentException
- if the object is null
or doesn't have a Primary key defined
or it belongs to a Realm instance in a different thread.copyToRealm(RealmModel)
public <E extends RealmModel> java.util.List<E> copyToRealm(java.lang.Iterable<E> objects)
objects
- the RealmObjects to copy to the Realm.RealmException
- if any of the objects has already been added to Realm.java.lang.IllegalArgumentException
- if any of the elements in the input collection is null
.public <E extends RealmModel> java.util.List<E> copyToRealmOrUpdate(java.lang.Iterable<E> objects)
PrimaryKey
or
creates a new copy if no existing object could be found. This is a deep copy or update, so all referenced objects
will be either copied or updated.
Please note, copying an object will copy all field values. All unset fields in this and child objects will be
set to their default value if not provided.objects
- a list of objects to update or copy into Realm.java.lang.IllegalArgumentException
- if RealmObject is null
or doesn't have a Primary key defined.copyToRealm(Iterable)
public <E extends RealmModel> java.util.List<E> copyFromRealm(java.lang.Iterable<E> realmObjects)
copyToRealmOrUpdate(RealmModel)
,
but all fields will be overridden, not just those that were changed. This includes references to other objects,
and can potentially override changes made by other threads.E
- type of object.realmObjects
- RealmObjects to copyjava.lang.IllegalArgumentException
- if the RealmObject is no longer accessible or it is a DynamicRealmObject
.copyToRealmOrUpdate(Iterable)
public <E extends RealmModel> java.util.List<E> copyFromRealm(java.lang.Iterable<E> realmObjects, int maxDepth)
copyToRealmOrUpdate(Iterable)
,
but all fields will be overridden, not just those that were changed. This includes references to other objects
even though they might be null
due to maxDepth
being reached. This can also potentially override
changes made by other threads.E
- type of object.realmObjects
- RealmObjects to copy.maxDepth
- limit of the deep copy. All references after this depth will be null
. Starting depth is
0
.java.lang.IllegalArgumentException
- if maxDepth < 0
, the RealmObject is no longer accessible or it is a
DynamicRealmObject
.copyToRealmOrUpdate(Iterable)
public <E extends RealmModel> E copyFromRealm(E realmObject)
RealmObject
. This is a deep copy that will copy
all referenced objects.
The copied object(s) are all detached from Realm so they will no longer be automatically updated. This means
that the copied objects might contain data that are no longer consistent with other managed Realm objects.
*WARNING*: Any changes to copied objects can be merged back into Realm using
copyToRealmOrUpdate(RealmModel)
, but all fields will be overridden, not just those that were changed.
This includes references to other objects, and can potentially override changes made by other threads.E
- type of object.realmObject
- RealmObject
to copyRealmObject
.java.lang.IllegalArgumentException
- if the RealmObject is no longer accessible or it is a DynamicRealmObject
.copyToRealmOrUpdate(RealmModel)
public <E extends RealmModel> E copyFromRealm(E realmObject, int maxDepth)
RealmObject
. This is a deep copy that will copy
all referenced objects up to the defined depth.
The copied object(s) are all detached from Realm so they will no longer be automatically updated. This means
that the copied objects might contain data that are no longer consistent with other managed Realm objects.
*WARNING*: Any changes to copied objects can be merged back into Realm using
copyToRealmOrUpdate(RealmModel)
, but all fields will be overridden, not just those that were changed.
This includes references to other objects even though they might be null
due to maxDepth
being
reached. This can also potentially override changes made by other threads.E
- type of object.realmObject
- RealmObject
to copymaxDepth
- limit of the deep copy. All references after this depth will be null
. Starting depth is
0
.RealmObject
.java.lang.IllegalArgumentException
- if maxDepth < 0
, the RealmObject is no longer accessible or it is a
DynamicRealmObject
.copyToRealmOrUpdate(RealmModel)
public <E extends RealmModel> RealmQuery<E> where(java.lang.Class<E> clazz)
clazz
- the class of the object which is to be queried for.RealmQuery
public void addChangeListener(RealmChangeListener<Realm> 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 <E extends RealmModel> RealmResults<E> allObjects(java.lang.Class<E> clazz)
realm.where(clazz).findAll()
instead.@Deprecated public <E extends RealmModel> RealmResults<E> allObjectsSorted(java.lang.Class<E> clazz, java.lang.String fieldName, Sort sortOrder)
realm.where(clazz).findAllSorted(fieldName, sortOrder)
instead.@Deprecated public <E extends RealmModel> RealmResults<E> allObjectsSorted(java.lang.Class<E> clazz, java.lang.String fieldName1, Sort sortOrder1, java.lang.String fieldName2, Sort sortOrder2)
realm.where(clazz).findAllSorted(fieldName1, sortOrder1, fieldName2, sortOrder2)
instead.@Deprecated public <E extends RealmModel> RealmResults<E> allObjectsSorted(java.lang.Class<E> clazz, java.lang.String fieldName1, Sort sortOrder1, java.lang.String fieldName2, Sort sortOrder2, java.lang.String fieldName3, Sort sortOrder3)
realm.where(clazz).findAllSorted(fieldName1, sortOrder1, fieldName2, sortOrder2, fieldName3, sortOrder3)
instead.@Deprecated public <E extends RealmModel> RealmResults<E> allObjectsSorted(java.lang.Class<E> clazz, java.lang.String[] fieldNames, Sort[] sortOrders)
realm.where(clazz).findAllSorted(fieldNames[], sortOrders[])
instead.@Deprecated public <E extends RealmModel> RealmResults<E> distinct(java.lang.Class<E> clazz, java.lang.String fieldName)
realm.where(clazz).distinct(fieldName)
instead.@Deprecated public <E extends RealmModel> RealmResults<E> distinctAsync(java.lang.Class<E> clazz, java.lang.String fieldName)
realm.where(clazz).distinctAsync(fieldName)
instead.@Deprecated public <E extends RealmObject> RealmResults<E> distinct(java.lang.Class<E> clazz, java.lang.String firstFieldName, java.lang.String... remainingFieldNames)
realm.where(clazz).distinct(firstFieldName, remainingFieldNames)
instead.public void executeTransaction(Realm.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
- the Realm.Transaction
to execute.java.lang.IllegalArgumentException
- if the transaction
is null
.@Deprecated public RealmAsyncTask executeTransaction(Realm.Transaction transaction, Realm.Transaction.Callback callback)
executeTransactionAsync(Transaction)
,
executeTransactionAsync(Transaction, Transaction.OnSuccess)
,
executeTransactionAsync(Transaction, io.realm.Realm.Transaction.OnError)
and
executeTransactionAsync(Transaction, Transaction.OnSuccess, Transaction.OnError)
.executeTransaction(Transaction)
but runs asynchronously on a worker thread.transaction
- Realm.Transaction
to execute.callback
- optional, to receive the result of this query.RealmAsyncTask
representing a cancellable task.java.lang.IllegalArgumentException
- if the transaction
is null
, or if the realm is opened from
another thread.public RealmAsyncTask executeTransactionAsync(Realm.Transaction transaction)
executeTransaction(Transaction)
but runs asynchronously on a worker thread.transaction
- Realm.Transaction
to execute.RealmAsyncTask
representing a cancellable task.java.lang.IllegalArgumentException
- if the transaction
is null
, or if the realm is opened from
another thread.public RealmAsyncTask executeTransactionAsync(Realm.Transaction transaction, Realm.Transaction.OnSuccess onSuccess)
executeTransactionAsync(Transaction)
, but also accepts an OnSuccess callback.transaction
- Realm.Transaction
to execute.onSuccess
- callback invoked when the transaction succeeds.RealmAsyncTask
representing a cancellable task.java.lang.IllegalArgumentException
- if the transaction
is null
, or if the realm is opened from
another thread.public RealmAsyncTask executeTransactionAsync(Realm.Transaction transaction, Realm.Transaction.OnError onError)
executeTransactionAsync(Transaction)
, but also accepts an OnError callback.transaction
- Realm.Transaction
to execute.onError
- callback invoked when the transaction failed.RealmAsyncTask
representing a cancellable task.java.lang.IllegalArgumentException
- if the transaction
is null
, or if the realm is opened from
another thread.public RealmAsyncTask executeTransactionAsync(Realm.Transaction transaction, Realm.Transaction.OnSuccess onSuccess, Realm.Transaction.OnError onError)
executeTransactionAsync(Transaction)
, but also accepts an OnSuccess and OnError callbacks.transaction
- Realm.Transaction
to execute.onSuccess
- callback invoked when the transaction succeeds.onError
- callback invoked when the transaction failed.RealmAsyncTask
representing a cancellable task.java.lang.IllegalArgumentException
- if the transaction
is null
, or if the realm is opened from
another thread.@Deprecated public void clear(java.lang.Class<? extends RealmModel> clazz)
delete(Class)
instead.clazz
- the class which objects should be removed.java.lang.IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.public void delete(java.lang.Class<? extends RealmModel> clazz)
clazz
- the class which objects should be removed.java.lang.IllegalStateException
- if the corresponding Realm is closed or called from an incorrect thread.public static void migrateRealm(RealmConfiguration configuration) throws java.io.FileNotFoundException
configuration
- RealmConfiguration
java.io.FileNotFoundException
- if the Realm file doesn't exist.public static void migrateRealm(RealmConfiguration configuration, RealmMigration migration) throws java.io.FileNotFoundException
configuration
- theRealmConfiguration
.migration
- the RealmMigration
to run on the Realm. This will override any migration set on the
configuration.java.io.FileNotFoundException
- if the Realm file doesn't exist.public static boolean deleteRealm(RealmConfiguration configuration)
RealmConfiguration
from the filesystem.
The Realm must be unused and closed before calling this method.configuration
- a RealmConfiguration
.false
if a file could not be deleted. The failing file will be logged.public static boolean compactRealm(RealmConfiguration configuration)
The file must be closed before this method is called, otherwise false
will be returned.
The file system should have free space for at least a copy of the Realm file.
The Realm file is left untouched if any file operation fails.
configuration
- a RealmConfiguration
pointing to a Realm file.true
if successful, false
if any file operation failed.java.lang.IllegalArgumentException
- if the realm file is encrypted. Compacting an encrypted Realm file is not
supported yet.public static java.lang.Object getDefaultModule()
RealmException
- if unable to create an instance of the module.RealmConfiguration.Builder.modules(Object, Object...)
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.