public final class Realm
extends java.lang.Object
implements java.io.Closeable
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(android.content.Context)
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 RealmActivity extends Activity { private Realm realm; \@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main); realm = Realm.getInstance(this); } \@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 |
protected static java.util.Map<android.os.Handler,java.lang.String> |
handlers |
protected static java.lang.ThreadLocal<java.util.Map<RealmConfiguration,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> |
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 specific 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 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<? extends RealmObject> clazz)
Remove all objects of the specified class.
|
void |
close()
Closes the Realm instance and all its resources.
|
void |
commitTransaction()
All changes since
beginTransaction() are persisted to disk and the
Realm reverts back to being read-only. |
static boolean |
compactRealm(RealmConfiguration configuration)
Compact a Realm file.
|
static boolean |
compactRealmFile(android.content.Context context)
Deprecated.
|
static boolean |
compactRealmFile(android.content.Context context,
java.lang.String fileName)
Deprecated.
|
<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> |
copyToRealmOrUpdate(E object)
Updates an existing RealmObject that is identified by the same
PrimaryKey or create
a new copy if no existing object could be found. |
<E extends RealmObject> |
copyToRealmOrUpdate(java.lang.Iterable<E> objects)
Updates a list of existing RealmObjects that is identified by their
PrimaryKey or create a
new copy if no existing object could be found. |
<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 pre-filled with data from a JSON object.
|
<E extends RealmObject> |
createObjectFromJson(java.lang.Class<E> clazz,
org.json.JSONObject json)
Create a Realm object pre-filled with data from a JSON object.
|
<E extends RealmObject> |
createObjectFromJson(java.lang.Class<E> clazz,
java.lang.String json)
Create a Realm object pre-filled with data from a JSON object.
|
<E extends RealmObject> |
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 RealmObject> |
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 RealmObject> |
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 RealmObject> |
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 RealmObject> |
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 RealmObject> |
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.
|
static boolean |
deleteRealm(RealmConfiguration configuration)
Delete the Realm file specified by the given
RealmConfiguration from the filesystem. |
static boolean |
deleteRealmFile(android.content.Context context)
Deprecated.
|
static boolean |
deleteRealmFile(android.content.Context context,
java.lang.String fileName)
Deprecated.
|
void |
executeTransaction(Realm.Transaction transaction)
Executes a given transaction on the Realm.
|
protected void |
finalize() |
protected java.util.List<java.lang.ref.WeakReference<RealmChangeListener>> |
getChangeListeners()
Return change listeners
For internal testing purpose only
|
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)
Realm static constructor for the default Realm "default.realm".
|
static Realm |
getInstance(android.content.Context context,
byte[] key)
Deprecated.
|
static Realm |
getInstance(android.content.Context context,
java.lang.String fileName)
Deprecated.
|
static Realm |
getInstance(android.content.Context context,
java.lang.String fileName,
byte[] key)
Deprecated.
|
static Realm |
getInstance(java.io.File writableFolder)
Deprecated.
|
static Realm |
getInstance(java.io.File writableFolder,
byte[] key)
Deprecated.
|
static Realm |
getInstance(java.io.File writableFolder,
java.lang.String fileName)
Deprecated.
|
static Realm |
getInstance(java.io.File writableFolder,
java.lang.String fileName,
byte[] key)
Deprecated.
|
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.
|
Table |
getTable(java.lang.Class<? extends RealmObject> clazz) |
boolean |
isAutoRefresh()
Retrieve the auto-refresh status of the Realm instance.
|
static void |
migrateRealm(RealmConfiguration configuration)
Manually trigger the migration associated with a given RealmConfiguration.
|
static void |
migrateRealm(RealmConfiguration configuration,
RealmMigration migration)
Manually trigger a migration on a RealmMigration.
|
static void |
migrateRealmAtPath(java.lang.String realmPath,
byte[] key,
RealmMigration migration)
Deprecated.
|
static void |
migrateRealmAtPath(java.lang.String realmPath,
byte[] key,
RealmMigration migration,
boolean autoUpdate)
Deprecated.
|
static void |
migrateRealmAtPath(java.lang.String realmPath,
RealmMigration migration)
Deprecated.
|
static void |
migrateRealmAtPath(java.lang.String realmPath,
RealmMigration migration,
boolean autoRefresh)
Deprecated.
|
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
|
static void |
removeDefaultConfiguration()
Removes the current default configuration (if any).
|
void |
setAutoRefresh(boolean autoRefresh)
Set the auto-refresh status of the Realm instance.
|
static void |
setDefaultConfiguration(RealmConfiguration configuration)
Sets the
RealmConfiguration used when calling getDefaultInstance() . |
<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<RealmConfiguration,Realm>> realmsCache
protected static final java.util.Map<android.os.Handler,java.lang.String> handlers
protected void checkIfValid()
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
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 isAutoRefresh()
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 trying to enable auto-refresh in a thread without Looper.public Table getTable(java.lang.Class<? extends RealmObject> clazz)
public static Realm getInstance(android.content.Context context)
close()
must be called when you are done using the Realm instance.
It sets auto-refresh on if the current thread has a Looper, off otherwise.
This is equivalent to calling Realm.getInstance(new RealmConfiguration(getContext()).build())
.
context
- an Android Context
RealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errors@Deprecated public static Realm getInstance(android.content.Context context, java.lang.String fileName)
close()
must be called when you are done using the Realm instance.
It sets auto-refresh on if the current thread has a Looper, off otherwise.
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 errors@Deprecated public static Realm getInstance(android.content.Context context, byte[] key)
close()
must be called when you are done using the Realm instance.
It sets auto-refresh on if the current thread has a Looper, off otherwise.
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 errors@Deprecated public static Realm getInstance(android.content.Context context, java.lang.String fileName, byte[] key)
close()
must be called when you are done using the Realm instance.
It sets auto-refresh on if the current thread has a Looper, off otherwise.
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 errors@Deprecated public static Realm getInstance(java.io.File writableFolder)
close()
must be called when you are done using the Realm instance.
It sets auto-refresh on if the current thread has a Looper, off otherwise.
writableFolder
- a File object representing a writable folderRealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errors@Deprecated public static Realm getInstance(java.io.File writableFolder, java.lang.String fileName)
close()
It sets auto-refresh on if the current thread has a Looper, off otherwise.writableFolder
- a File object representing a writable 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 errors@Deprecated public static Realm getInstance(java.io.File writableFolder, byte[] key)
close()
must be called when you are done using the Realm instance.
It sets auto-refresh on if the current thread has a Looper, off otherwise.
writableFolder
- a File object representing a writable folderkey
- a 64-byte encryption keyRealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying fileRealmException
- Other errors@Deprecated public static Realm getInstance(java.io.File writableFolder, java.lang.String fileName, byte[] key)
close()
must be called when you are done using the Realm instance.
It sets auto-refresh on if the current thread has a Looper, off otherwise.
writableFolder
- a File object representing a writable 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 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
model classes or version has has changed so a migration is required.public static Realm getInstance(RealmConfiguration configuration)
RealmConfiguration
RealmMigrationNeededException
- If no migration has been provided by the configuration and the
model classes or version has has changed so a migration is required.for details on how to configure a Realm.
public static void setDefaultConfiguration(RealmConfiguration configuration)
RealmConfiguration
used when calling getDefaultInstance()
.configuration
- RealmConfiguration to use as the default configuration.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 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 createOrUpdateAllFromJson(java.lang.Class<E> clazz, org.json.JSONArray json)
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
.createAllFromJson(Class, org.json.JSONArray)
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 createOrUpdateAllFromJson(java.lang.Class<E> clazz, java.lang.String json)
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
.createAllFromJson(Class, String)
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> void createOrUpdateAllFromJson(java.lang.Class<E> clazz, java.io.InputStream in) throws java.io.IOException
clazz
- Type of RealmObject
to create or update. It must have a primary key defined.in
- InputStream with a list of object data in JSON format.java.lang.IllegalArgumentException
- if trying to update a class without a
PrimaryKey
.java.io.IOException
createOrUpdateAllFromJson(Class, java.io.InputStream)
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.createOrUpdateObjectFromJson(Class, org.json.JSONObject)
public <E extends RealmObject> E createOrUpdateObjectFromJson(java.lang.Class<E> clazz, org.json.JSONObject json)
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
.createObjectFromJson(Class, org.json.JSONObject)
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 createOrUpdateObjectFromJson(java.lang.Class<E> clazz, java.lang.String json)
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
.createObjectFromJson(Class, String)
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 <E extends RealmObject> E createOrUpdateObjectFromJson(java.lang.Class<E> clazz, java.io.InputStream in) throws java.io.IOException
clazz
- Type of RealmObject
to create or update. It must have a primary key defined.in
- InputStream
with object data in JSON format.RealmObject
.java.lang.IllegalArgumentException
- if trying to update a class without a
PrimaryKey
.java.io.IOException
createObjectFromJson(Class, java.io.InputStream)
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 write transaction it writes the current data, and not the data as it was when the last write transaction was committed.
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
The destination file cannot already exist.
Note that if this is called from within a write transaction it writes the current data, and not the data as it was when the last write transaction was committed.
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.java.lang.IllegalArgumentException
- if RealmObject is null
.public <E extends RealmObject> E copyToRealmOrUpdate(E object)
PrimaryKey
or create
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.object
- RealmObject
to copy or update.java.lang.IllegalArgumentException
- if RealmObject is null
or doesn't have a Primary key defined.copyToRealm(RealmObject)
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> java.util.List<E> copyToRealmOrUpdate(java.lang.Iterable<E> objects)
PrimaryKey
or create 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.objects
- 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 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
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.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.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.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 field names 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
protected java.util.List<java.lang.ref.WeakReference<RealmChangeListener>> getChangeListeners()
public void refresh()
public void beginTransaction()
commitTransaction()
or aborted by cancelTransaction()
. Write transactions are used to
atomically create, update and delete objects within a realm.
beginTransaction()
updates the
realm in the case of pending updates from other threads.
java.lang.IllegalStateException
- If already in a write transaction or incorrect thread.public void commitTransaction()
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.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<? extends RealmObject> clazz)
clazz
- The class which objects should be removedjava.lang.RuntimeException
- Any other error@Deprecated public static void migrateRealmAtPath(java.lang.String realmPath, RealmMigration migration)
@Deprecated public static void migrateRealmAtPath(java.lang.String realmPath, byte[] key, RealmMigration migration)
@Deprecated public static void migrateRealmAtPath(java.lang.String realmPath, RealmMigration migration, boolean autoRefresh)
@Deprecated public static void migrateRealmAtPath(java.lang.String realmPath, byte[] key, RealmMigration migration, boolean autoUpdate)
public static void migrateRealm(RealmConfiguration configuration)
configuration
- public static void migrateRealm(RealmConfiguration configuration, RealmMigration migration)
configuration
- RealmConfiguration
migration
- RealmMigration
to run on the Realm. This will override any migration set on the
configuration.@Deprecated public static boolean deleteRealmFile(android.content.Context context)
deleteRealm(RealmConfiguration)
instead.
Delete the Realm file from the filesystem for the default Realm (named "default.realm").
The Realm must be unused and closed before calling this method.
WARNING: Your Realm must not be open (typically when your app launch).context
- an Android Context
.java.lang.IllegalStateException
- if trying to delete a Realm that is already open.clear(Class)
@Deprecated public static boolean deleteRealmFile(android.content.Context context, java.lang.String fileName)
deleteRealm(RealmConfiguration)
instead.
Delete the Realm file from the filesystem for a custom named Realm.
The Realm must be unused and closed before calling this method.context
- an Android Context
.fileName
- the name of the custom Realm (i.e. "myCustomRealm.realm").java.lang.IllegalStateException
- if trying to delete a Realm that is already open.public static boolean deleteRealm(RealmConfiguration configuration)
RealmConfiguration
from the filesystem.
The Realm must be unused and closed before calling this method.configuration
- A RealmConfiguration
java.lang.IllegalStateException
- if trying to delete a Realm that is already open.@Deprecated public static boolean compactRealmFile(android.content.Context context, java.lang.String fileName)
compactRealm(RealmConfiguration)
instead.
Compact a Realm file. A Realm file usually contain free/unused space.
This method removes this free space and the file size is thereby reduced.
Objects within the Realm files are untouched.
The file must be closed before this method is called.
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.
Currently it is not possible to compact an encrypted Realm.
context
- an Android Context
fileName
- the name of the file to compactjava.lang.IllegalStateException
- if trying to compact a Realm that is already open.@Deprecated public static boolean compactRealmFile(android.content.Context context)
compactRealm(RealmConfiguration)
instead.
Compact a Realm file. A Realm file usually contain free/unused space.
This method removes this free space and the file size is thereby reduced.
Objects within the Realm files are untouched.
The file must be closed before this method is called.
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.
context
- an Android Context
java.lang.IllegalStateException
- if trying to compact a Realm that is already open.public static boolean compactRealm(RealmConfiguration configuration)
The file must be closed before this method is called.
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.java.lang.IllegalStateException
- if trying to compact a Realm that is already open.public java.lang.String getPath()
File.getCanonicalPath()
public RealmConfiguration getConfiguration()
RealmConfiguration
for this Realm.RealmConfiguration
for this Realm.public static java.lang.Object getDefaultModule()
RealmConfiguration.Builder.setModules(Object, Object...)