public final class Realm
extends java.lang.Object
implements java.io.Closeable
The Realm class is the storage and transactional manager of your object persistent store. It is in charge of creating instances of your RealmObjects. Objects within a Realm can be queried and read at any time. Creating, modifying, and deleting objects must be done through transactions.
The transactions ensure that multiple instances (on multiple threads) can access the objects in a consistent state with full ACID guaranties.
If auto-refresh is set the instance of the Realm will be automatically updated when another instance commits a change (create, modify or delete an object). This feature requires the Realm instance to be residing in a thread attached to a Looper (the main thread has a Looper by default)
For normal threads Android provides a utility class that can be used like this:
HandlerThread thread = new HandlerThread("MyThread") { \@Override protected void onLooperPrepared() { Realm realm = Realm.getInstance(getContext()); // This realm will be updated by the event loop // on every commit performed by other realm instances } }; thread.start();It is important to remember to call the close() method when done with the Realm instance.
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 |
protected static java.util.Map<android.os.Handler,java.lang.Integer> |
handlers |
protected static java.lang.ThreadLocal<java.util.Map<java.lang.Integer,Realm>> |
realmsCache |
Modifier and Type | Method and Description |
---|---|
void |
addChangeListener(RealmChangeListener listener)
Add a change listener to the Realm
|
<E extends RealmObject> |
allObjects(java.lang.Class<E> clazz)
Get all objects of a specific Class
|
<E extends RealmObject> |
allObjects(java.lang.Class<E> clazz,
java.lang.String[] fieldNames,
boolean[] sortAscending)
Deprecated.
|
<E extends RealmObject> |
allObjects(java.lang.Class<E> clazz,
java.lang.String fieldName,
boolean sortAscending)
Deprecated.
|
<E extends RealmObject> |
allObjects(java.lang.Class<E> clazz,
java.lang.String fieldName1,
boolean sortAscending1,
java.lang.String fieldName2,
boolean sortAscending2)
Deprecated.
|
<E extends RealmObject> |
allObjects(java.lang.Class<E> clazz,
java.lang.String fieldName1,
boolean sortAscending1,
java.lang.String fieldName2,
boolean sortAscending2,
java.lang.String fieldName3,
boolean sortAscending3)
Deprecated.
|
<E extends RealmObject> |
allObjectsSorted(java.lang.Class<E> clazz,
java.lang.String[] fieldNames,
boolean[] sortAscending)
Get all objects of a specific Class sorted by multiple fields.
|
<E extends RealmObject> |
allObjectsSorted(java.lang.Class<E> clazz,
java.lang.String fieldName,
boolean sortAscending)
Get all objects of a specific Class sorted by a field.
|
<E extends RealmObject> |
allObjectsSorted(java.lang.Class<E> clazz,
java.lang.String fieldName1,
boolean sortAscending1,
java.lang.String fieldName2,
boolean sortAscending2)
Get all objects of a specific class sorted by two field names.
|
<E extends RealmObject> |
allObjectsSorted(java.lang.Class<E> clazz,
java.lang.String fieldName1,
boolean sortAscending1,
java.lang.String fieldName2,
boolean sortAscending2,
java.lang.String fieldName3,
boolean sortAscending3)
Get all objects of a specific class sorted by two specific field names.
|
void |
beginTransaction()
Starts a write transaction, this must be closed with
commitTransaction()
or aborted by @{link io.realm.Realm#cancelTransaction()}. |
void |
cancelTransaction()
Revert all writes (created, updated, or deleted objects) made in the current write
transaction and end the transaction.
|
protected void |
checkIfValid() |
void |
clear(java.lang.Class<?> classSpec)
Remove all objects of the specified class.
|
void |
close()
Closes the Realm instance and all its resources.
|
void |
commitTransaction()
All changes since @{link io.realm.Realm#beginTransaction()} are persisted to disk and the
realm reverts back to being read-only.
|
static boolean |
compactRealmFile(android.content.Context context)
Compact a realm file.
|
static boolean |
compactRealmFile(android.content.Context context,
java.lang.String fileName)
Compact a realm file.
|
<E extends RealmObject> |
copyToRealm(E object)
Copies a RealmObject to the Realm instance and returns the copy.
|
<E extends RealmObject> |
copyToRealm(java.lang.Iterable<E> objects)
Copies a collection of RealmObjects to the Realm instance and returns their copy.
|
<E extends RealmObject> |
createAllFromJson(java.lang.Class<E> clazz,
java.io.InputStream inputStream)
Create a Realm object for each object in a JSON array.
|
<E extends RealmObject> |
createAllFromJson(java.lang.Class<E> clazz,
org.json.JSONArray json)
Create a Realm object for each object in a JSON array.
|
<E extends RealmObject> |
createAllFromJson(java.lang.Class<E> clazz,
java.lang.String json)
Create a Realm object for each object in a JSON array.
|
<E extends RealmObject> |
createObject(java.lang.Class<E> clazz)
Instantiates and adds a new object to the realm
|
<E extends RealmObject> |
createObjectFromJson(java.lang.Class<E> clazz,
java.io.InputStream inputStream)
Create a Realm object prefilled with data from a JSON object.
|
<E extends RealmObject> |
createObjectFromJson(java.lang.Class<E> clazz,
org.json.JSONObject json)
Create a Realm object prefilled with data from a JSON object.
|
<E extends RealmObject> |
createObjectFromJson(java.lang.Class<E> clazz,
java.lang.String json)
Create a Realm object prefilled with data from a JSON object.
|
static boolean |
deleteRealmFile(android.content.Context context)
Delete the Realm file from the filesystem for the default Realm (named "default.realm").
|
static boolean |
deleteRealmFile(android.content.Context context,
java.lang.String fileName)
Delete the Realm file from the filesystem for a custom named Realm.
|
void |
executeTransaction(Realm.Transaction transaction)
Executes a given transaction on the Realm.
|
protected void |
finalize() |
static Realm |
getInstance(android.content.Context context)
Realm static constructor for the default realm "default.realm".
|
static Realm |
getInstance(android.content.Context context,
byte[] key)
Realm static constructor.
|
static Realm |
getInstance(android.content.Context context,
java.lang.String fileName)
Realm static constructor.
|
static Realm |
getInstance(android.content.Context context,
java.lang.String fileName,
byte[] key)
Realm static constructor.
|
static Realm |
getInstance(java.io.File writeableFolder)
Realm static constructor.
|
static Realm |
getInstance(java.io.File writeableFolder,
byte[] key)
Realm static constructor.
|
static Realm |
getInstance(java.io.File writeableFolder,
java.lang.String fileName)
Realm static constructor.
|
static Realm |
getInstance(java.io.File writeableFolder,
java.lang.String fileName,
byte[] key)
Realm static constructor.
|
java.lang.String |
getPath()
Returns the absolute path to where this Realm is persisted on disk.
|
Table |
getTable(java.lang.Class<?> clazz) |
boolean |
isAutoRefresh()
Retrieve the auto-refresh status of the Realm instance.
|
static void |
migrateRealmAtPath(java.lang.String realmPath,
byte[] key,
RealmMigration migration) |
static void |
migrateRealmAtPath(java.lang.String realmPath,
byte[] key,
RealmMigration migration,
boolean autoUpdate) |
static void |
migrateRealmAtPath(java.lang.String realmPath,
RealmMigration migration) |
static void |
migrateRealmAtPath(java.lang.String realmPath,
RealmMigration migration,
boolean autoRefresh) |
void |
refresh()
Refresh the Realm instance and all the RealmResults and RealmObjects instances coming from it
|
void |
removeAllChangeListeners()
Remove all user-defined change listeners
|
void |
removeChangeListener(RealmChangeListener listener)
Remove the specified change listener
|
void |
setAutoRefresh(boolean autoRefresh)
Set the auto-refresh status of the Realm instance.
|
<E extends RealmObject> |
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)
Write a compacted copy of the Realm to the given destination File.
|
void |
writeEncryptedCopyTo(java.io.File destination,
byte[] key)
Write a compacted and encrypted copy of the Realm to the given destination File.
|
public static final java.lang.String DEFAULT_REALM_NAME
protected static final java.lang.ThreadLocal<java.util.Map<java.lang.Integer,Realm>> realmsCache
protected static final java.util.Map<android.os.Handler,java.lang.Integer> handlers
protected void checkIfValid()
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public boolean isAutoRefresh()
public void setAutoRefresh(boolean autoRefresh)
Looper
enabled thread.autoRefresh
- true will turn auto-refresh on, false will turn it off.java.lang.IllegalStateException
- if trying to enable auto-refresh in a thread without Looper.public Table getTable(java.lang.Class<?> clazz)
public static Realm getInstance(android.content.Context context)
context
- an Android Context
RealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errorspublic static Realm getInstance(android.content.Context context, java.lang.String fileName)
context
- an Android Context
fileName
- the name of the file to save the Realm toRealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errorspublic static Realm getInstance(android.content.Context context, byte[] key)
context
- an Android Context
key
- a 64-byte encryption keyRealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errorspublic static Realm getInstance(android.content.Context context, java.lang.String fileName, byte[] key)
context
- an Android Context
key
- a 64-byte encryption keyRealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errorspublic static Realm getInstance(java.io.File writeableFolder)
writeableFolder
- a File object representing a writeable folderRealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errorspublic static Realm getInstance(java.io.File writeableFolder, java.lang.String fileName)
writeableFolder
- a File object representing a writeable folderfileName
- the name of the Realm fileRealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errorspublic static Realm getInstance(java.io.File writeableFolder, byte[] key)
writeableFolder
- a File object representing a writeable folderkey
- a 64-byte encryption keyRealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errorspublic static Realm getInstance(java.io.File writeableFolder, java.lang.String fileName, byte[] key)
writeableFolder
- a File object representing a writeable folderfileName
- the name of the Realm filekey
- a 64-byte encryption keyRealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errorspublic <E extends RealmObject> void createAllFromJson(java.lang.Class<E> clazz, org.json.JSONArray json)
clazz
- Type of Realm objects to create.json
- Array where each JSONObject must map to the specified class.RealmException
- if mapping from JSON fails.public <E extends RealmObject> void createAllFromJson(java.lang.Class<E> clazz, java.lang.String json)
clazz
- Type of Realm objects to create.json
- JSON array as a String where each object can map to the specified class.RealmException
- if mapping from JSON fails.public <E extends RealmObject> void createAllFromJson(java.lang.Class<E> clazz, java.io.InputStream inputStream) throws java.io.IOException
clazz
- Type of Realm objects created.inputStream
- 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 RealmObject> E createObjectFromJson(java.lang.Class<E> clazz, org.json.JSONObject json)
clazz
- Type of Realm object to create.json
- JSONObject with object data.RealmException
- if the mapping from JSON fails.public <E extends RealmObject> E createObjectFromJson(java.lang.Class<E> clazz, java.lang.String json)
clazz
- Type of Realm object to create.json
- JSON string with object data.RealmException
- if mapping to json failed.public <E extends RealmObject> E createObjectFromJson(java.lang.Class<E> clazz, java.io.InputStream inputStream) throws java.io.IOException
clazz
- Type of Realm object to create.inputStream
- 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 void writeCopyTo(java.io.File destination) throws java.io.IOException
destination
- File to save the Realm tojava.io.IOException
- if any write operation failspublic void writeEncryptedCopyTo(java.io.File destination, byte[] key) throws java.io.IOException
destination
- File to save the Realm tojava.io.IOException
- if any write operation failspublic <E extends RealmObject> E createObject(java.lang.Class<E> clazz)
clazz
- The Class of the object to createRealmException
- An object could not be createdpublic <E extends RealmObject> E copyToRealm(E object)
object
- RealmObject
to copy to the Realm.RealmException
- if the RealmObject has already been added to the Realm.java.lang.IllegalArgumentException
- if RealmObject is null
.public <E extends RealmObject> java.util.List<E> copyToRealm(java.lang.Iterable<E> objects)
objects
- 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 RealmObject> RealmQuery<E> where(java.lang.Class<E> clazz)
clazz
- The class of the object which is to be queried forjava.lang.RuntimeException
- Any other errorRealmQuery
public <E extends RealmObject> RealmResults<E> allObjects(java.lang.Class<E> clazz)
clazz
- the Class to get objects ofjava.lang.RuntimeException
- Any other errorRealmResults
@Deprecated public <E extends RealmObject> RealmResults<E> allObjects(java.lang.Class<E> clazz, java.lang.String fieldName, boolean sortAscending)
clazz
- the Class to get objects of.fieldName
- the field name to sort by.sortAscending
- sort ascending if SORT_ORDER_ASCENDING, sort descending if SORT_ORDER_DESCENDING.java.lang.IllegalArgumentException
- if field name does not exist.public <E extends RealmObject> RealmResults<E> allObjectsSorted(java.lang.Class<E> clazz, java.lang.String fieldName, boolean sortAscending)
clazz
- the Class to get objects of.fieldName
- the field name to sort by.sortAscending
- sort ascending if SORT_ORDER_ASCENDING, sort descending if SORT_ORDER_DESCENDING.java.lang.IllegalArgumentException
- if field name does not exist.@Deprecated public <E extends RealmObject> RealmResults<E> allObjects(java.lang.Class<E> clazz, java.lang.String fieldName1, boolean sortAscending1, java.lang.String fieldName2, boolean sortAscending2)
clazz
- the class ti get objects of.fieldName1
- first field name to sort by.sortAscending1
- sort order for first field.fieldName2
- second field name to sort by.sortAscending2
- sort order for second field.java.lang.IllegalArgumentException
- if a field name does not exist.public <E extends RealmObject> RealmResults<E> allObjectsSorted(java.lang.Class<E> clazz, java.lang.String fieldName1, boolean sortAscending1, java.lang.String fieldName2, boolean sortAscending2)
clazz
- the class ti get objects of.fieldName1
- first field name to sort by.sortAscending1
- sort order for first field.fieldName2
- second field name to sort by.sortAscending2
- sort order for second field.java.lang.IllegalArgumentException
- if a field name does not exist.@Deprecated public <E extends RealmObject> RealmResults<E> allObjects(java.lang.Class<E> clazz, java.lang.String fieldName1, boolean sortAscending1, java.lang.String fieldName2, boolean sortAscending2, java.lang.String fieldName3, boolean sortAscending3)
clazz
- the class ti get objects of.fieldName1
- first field name to sort by.sortAscending1
- sort order for first field.fieldName2
- second field name to sort by.sortAscending2
- sort order for second field.fieldName3
- third field name to sort by.sortAscending3
- sort order for third field.java.lang.IllegalArgumentException
- if a field name does not exist.public <E extends RealmObject> RealmResults<E> allObjectsSorted(java.lang.Class<E> clazz, java.lang.String fieldName1, boolean sortAscending1, java.lang.String fieldName2, boolean sortAscending2, java.lang.String fieldName3, boolean sortAscending3)
clazz
- the class ti get objects of.fieldName1
- first field name to sort by.sortAscending1
- sort order for first field.fieldName2
- second field name to sort by.sortAscending2
- sort order for second field.fieldName3
- third field name to sort by.sortAscending3
- sort order for third field.java.lang.IllegalArgumentException
- if a field name does not exist.@Deprecated public <E extends RealmObject> RealmResults<E> allObjects(java.lang.Class<E> clazz, java.lang.String[] fieldNames, boolean[] sortAscending)
clazz
- the Class to get objects of.sortAscending
- sort ascending if SORT_ORDER_ASCENDING, sort descending if SORT_ORDER_DESCENDING.fieldNames
- an array of fieldnames to sort objects by.
The objects are first sorted by fieldNames[0], then by fieldNames[1] and so forth.java.lang.IllegalArgumentException
- if a field name does not exist.public <E extends RealmObject> RealmResults<E> allObjectsSorted(java.lang.Class<E> clazz, java.lang.String[] fieldNames, boolean[] sortAscending)
clazz
- the Class to get objects of.sortAscending
- sort ascending if SORT_ORDER_ASCENDING, sort descending if SORT_ORDER_DESCENDING.fieldNames
- an array of fieldnames to sort objects by.
The objects are first sorted by fieldNames[0], then by fieldNames[1] and so forth.java.lang.IllegalArgumentException
- if a field name does not exist.public void addChangeListener(RealmChangeListener listener)
listener
- the change listenerRealmChangeListener
public void removeChangeListener(RealmChangeListener listener)
listener
- the change listener to be removedRealmChangeListener
public void removeAllChangeListeners()
RealmChangeListener
public void refresh()
public void beginTransaction()
commitTransaction()
or aborted by @{link io.realm.Realm#cancelTransaction()}. Write transactions are used to
atomically create, update and delete objects within a realm.
java.lang.IllegalStateException
- If already in a write transaction or incorrect thread.public void commitTransaction()
java.lang.IllegalStateException
- If the write transaction is in an invalid state or incorrect thread.public void cancelTransaction()
java.lang.IllegalStateException
- If the write transaction is an invalid state,
not in a write transaction or incorrect thread.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
- Realm.Transaction
to execute.RealmException
- if any error happened during the transaction.public void clear(java.lang.Class<?> classSpec)
classSpec
- The class which objects should be removedjava.lang.RuntimeException
- Any other errorpublic static void migrateRealmAtPath(java.lang.String realmPath, RealmMigration migration)
public static void migrateRealmAtPath(java.lang.String realmPath, byte[] key, RealmMigration migration)
public static void migrateRealmAtPath(java.lang.String realmPath, RealmMigration migration, boolean autoRefresh)
public static void migrateRealmAtPath(java.lang.String realmPath, byte[] key, RealmMigration migration, boolean autoUpdate)
public static boolean deleteRealmFile(android.content.Context context)
context
- an Android context.clear(Class)
public static boolean deleteRealmFile(android.content.Context context, java.lang.String fileName)
context
- an Android @{Context
.fileName
- the name of the custom Realm (i.e. "myCustomRealm.realm").public static boolean compactRealmFile(android.content.Context context, java.lang.String fileName)
context
- an Android Context
fileName
- the name of the file to compactpublic static boolean compactRealmFile(android.content.Context context)
context
- an Android Context
public java.lang.String getPath()