Navigation

Write Transactions - Android SDK

Realm Database uses a highly efficient storage engine to persist objects. You can create objects in a realm, update objects in a realm, and eventually delete objects from a realm. Because these operations modify the state of the realm, we call them writes.

Realm handles writes in terms of transactions. A transaction is a list of read and write operations that Realm treats as a single indivisible operation. In other words, a transaction is all or nothing: either all of the operations in the transaction succeed or none of the operations in the transaction take effect.

Note

All writes must happen in a transaction.

A realm allows only one open write transaction at a time. Realm blocks other writes on other threads until the open transaction is complete. Consequently, there is no race condition when reading values from the realm within a transaction.

When you are done with your transaction, Realm either commits it or cancels it:

  • When Realm commits a transaction, Realm writes all changes to disk. For synced realms, Realm queues the change for synchronization with MongoDB Realm.
  • When Realm cancels a write transaction or an operation in the transaction causes an error, all changes are discarded (or "rolled back").

Realm guarantees that transactions are ACID compliant. This means that all committed write operations are guaranteed to be valid and that clients don't see transient states in the event of a system crash.

  • Realm establishes atomicity by grouping operations in transactions and rolling back all operations in a transaction if any of them fail.
  • Realm establishes consistency and avoids data corruption by validating changes against the schema. If the result of any write operation is not valid, Realm cancels and rolls back the entire transaction.
  • Realm establishes isolation by allowing only one writer at a time. This ensures thread safety between transactions.
  • Finally, Realm establishes durability by writing to disk immediately when a transaction is committed. In the event of an app crash, for example, the changes are not lost or corrupted.

Realm represents each transaction as a callback function that contains zero or more read and write operations. To run a transaction, define a transaction callback and pass it to the realm's write method. Within this callback, you are free to create, read, update, and delete on the realm. If the code in the callback throws an exception when Realm runs it, Realm cancels the transaction. Otherwise, Realm commits the transaction immediately after the callback.

Example

The following code shows how to run a transaction with executeTransaction() or executeTransactionAsync(). If the code in the callback throws an exception, Realm cancels the transaction. Otherwise, Realm commits the transaction.

realm.executeTransaction { r: Realm ->
// Create a turtle enthusiast named Ali.
val ali = r.createObject(TurtleEnthusiast::class.java, ObjectId())
ali.name = "Ali"
// Find turtles younger than 2 years old
val hatchlings =
r.where(Turtle::class.java).lessThan("age", 2).findAll()
// Give all hatchlings to Ali.
hatchlings.setObject("owner", ali)
}
Tip
See also:
Give Feedback