public class Realm
extends java.lang.Object
The Realm class is the storage and transactional manager of your object persistent store. Objects are created. 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") {
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_REALM_NAME |
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
|
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.
|
void |
clear(java.lang.Class<?> classSpec)
Remove all objects of the specified class.
|
void |
commitTransaction()
All changes since @{link io.realm.Realm#beginTransaction()} are persisted to disk and the
realm reverts back to being read-only.
|
static Realm |
create(java.io.File writableFolder,
java.lang.String filename,
byte[] key,
boolean autoRefresh)
Realm static constructor
|
<E extends RealmObject> |
createObject(java.lang.Class<E> clazz)
Instantiates and adds a new object to the realm
|
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.
|
protected void |
finalize() |
static Realm |
getInstance(android.content.Context context)
Realm static constructor for the default realm "default.realm"
It sets auto-refresh on
|
static Realm |
getInstance(android.content.Context context,
boolean autoRefresh)
Realm static constructor for the default realm "default.realm"
|
static Realm |
getInstance(android.content.Context context,
byte[] key)
Realm static constructor
It sets auto-refresh on
|
static Realm |
getInstance(android.content.Context context,
byte[] key,
boolean autoRefresh)
Realm static constructor
|
static Realm |
getInstance(android.content.Context context,
java.lang.String fileName)
Realm static constructor
It sets auto-refresh on
|
static Realm |
getInstance(android.content.Context context,
java.lang.String fileName,
boolean autoRefresh)
Realm static constructor
|
static Realm |
getInstance(android.content.Context context,
java.lang.String fileName,
byte[] key,
boolean autoRefresh)
Realm static constructor
|
static Realm |
getInstance(java.io.File writableFolder,
byte[] key,
boolean autoRefresh)
Realm static constructor
|
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
Auto-refresh is a feature to allows to automatically update 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.
|
<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
|
public static final java.lang.String DEFAULT_REALM_NAME
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public boolean isAutoRefresh()
public void setAutoRefresh(boolean autoRefresh)
Looper
enabled thread.autoRefresh
- true will turn auto-refresh on, false will turn it offjava.lang.IllegalStateException
- if trying to enable auto-refresh in a thread without Looperpublic 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 filejava.lang.IllegalStateException
- The Realm is being instantiated in a Thread without
a Looper
RealmException
- Other errorspublic static Realm getInstance(android.content.Context context, boolean autoRefresh)
context
- an Android contextautoRefresh
- whether the Realm object and its derived objects (RealmResults and RealmObjects)
should be automatically refreshed with the event loop (requires to be in a thread with a Looper)RealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying filejava.lang.IllegalStateException
- The Realm is being instantiated with auto-refresh
in a Thread without a Looper
RealmException
- 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 filejava.lang.IllegalStateException
- The Realm is being instantiated in a Thread without
a Looper
RealmException
- Other errorspublic static Realm getInstance(android.content.Context context, java.lang.String fileName, boolean autoRefresh)
context
- an Android contextfileName
- the name of the file to save the Realm toautoRefresh
- whether the Realm object and its derived objects (RealmResults and RealmObjects)
should be automatically refreshed with the event loop (requires to be in a thread with a Looper)RealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying filejava.lang.IllegalStateException
- The Realm is being instantiated with auto-refresh
in a Thread without a Looper
RealmException
- Other errorspublic static Realm getInstance(android.content.Context context, byte[] key, boolean autoRefresh)
context
- an Android contextkey
- a 32-byte encryption keyautoRefresh
- whether the Realm object and its derived objects (RealmResults and RealmObjects)
should be automatically refreshed with the event loop (requires to be in a thread with a Looper)RealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying filejava.lang.IllegalStateException
- The Realm is being instantiated with auto-refresh
in a Thread without a Looper
RealmException
- Other errorspublic static Realm getInstance(android.content.Context context, byte[] key)
context
- an Android Context
key
- a 32-byte encryption keyRealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying filejava.lang.IllegalStateException
- The Realm is being instantiated in a Thread without
a Looper
RealmException
- Other errorspublic static Realm getInstance(android.content.Context context, java.lang.String fileName, byte[] key, boolean autoRefresh)
context
- an Android Context
fileName
- the name of the file to save the Realm tokey
- a 32-byte encryption keyautoRefresh
- whether the Realm object and its derived objects (RealmResults and RealmObjects)
should be automatically refreshed with the event loop (requires to be in a thread with a Looper)RealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying filejava.lang.IllegalStateException
- The Realm is being instantiated in a Thread without
a Looper
RealmException
- Other errorspublic static Realm getInstance(java.io.File writableFolder, byte[] key, boolean autoRefresh)
writableFolder
- absolute path to a writable directorykey
- a 32-byte encryption keyautoRefresh
- whether the Realm object and its derived objects (RealmResults and RealmObjects)
should be automatically refreshed with the event loop (requires to be in a thread with a Looper)RealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying filejava.lang.IllegalStateException
- The Realm is being instantiated in a Thread without
a Looper
RealmException
- Other errorspublic static Realm create(java.io.File writableFolder, java.lang.String filename, byte[] key, boolean autoRefresh)
writableFolder
- absolute path to a writable directoryfilename
- the name of the file to save the Realm tokey
- a 32-byte encryption keyautoRefresh
- whether the Realm object and its derived objects (RealmResults and RealmObjects)
should be automatically refreshed with the event loop (requires to be in a thread with a Looper)RealmMigrationNeededException
- The model classes have been changed and the Realm
must be migratedRealmIOException
- Error when accessing underlying filejava.lang.IllegalStateException
- The Realm is being instantiated in a Thread without
a Looper
RealmException
- Other errorspublic <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> 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 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.public void commitTransaction()
java.lang.IllegalStateException
- If the write transaction is in an invalid state.public void cancelTransaction()
java.lang.IllegalStateException
- If the write transaction is an invalid state or
not in a write 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").