Write Transactions - React Native SDK
On this page
A write transaction is a function that modifies objects in a realm. Write transactions let you create, modify, or delete Realm objects. They handle operations in a single, idempotent update. A transaction is all or nothing. Either:
- All the operations in the transaction succeed, or;
- If any operation fails, none of the operations complete.
Every write operation must occur in a write transaction.
Write transactions are callback functions that you pass to a realm instance. For examples of specific write operations, see Write Operations.
const realm = await Realm.open(); realm.write(() => { const tesla = realm.create("Car", { make: "Tesla", model: "Model S", year: 2020, miles: 12000 }) const honda = realm.create("Car", { make: "Honda", model: "Civic", year: 2018, miles: 30000 }) })
Transaction Lifecycle
A given realm only processes one write transaction at a time. When you make a write transaction, the realm adds the transaction to a queue. The realm evaluates each transaction in the order it arrived.
After processing a transaction, Realm either commits it or cancels it:
After a commit, the realm applies all operations in the transaction. Once applied, the realm automatically updates live queries. It notifies listeners of created, modified, and deleted objects.
NoteWhen using Sync, Realm also queues the changes to send to MongoDB Realm. Realm sends these changes when a network is available.
- Realm does not apply any operations in a cancelled transaction. Realm cancels a transaction if an operation fails or is invalid.