Docs Menu

Write Transactions - React Native SDK

On this page

  • Transaction Lifecycle

A write transaction is a function that can modify the state of objects in a given realm. Write transactions let you create, modify, or delete Realm objects and can encapsulate multiple operations into a single, idempotent update. In other words a transaction is all or nothing: either all of the operations in the transaction succeed or, if any operation fails, none of the operations in the transaction take effect.

Important

Every write operation must occur in a write transaction.

Example

Write transactions are callback functions that you pass to a realm instance to modify objects in that realm. 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
})
})

A given realm only processes one write transaction at a time. When a realm receives a write transaction, it adds the transaction to a queue and evaluates each transaction individually in the order they arrived.

After processing a transaction, Realm either commits it or cancels it:

  • Realm applies all operations in a committed transaction to the realm. Once applied, Realm automatically updates live queries and calls any notification listeners that refer to created, modified, and deleted objects.

    Note

    In synced realms, Realm also queues the change to be sent to MongoDB Realm whenever a network is available.

  • Realm does not apply any operations in a cancelled transaction to the realm. Realm cancels a transaction if any operation in the transaction is invalid or fails.
Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.