Skip to content

(DOCSP-26996) Add Realm React support to 'Write Transactions' #2560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 16 additions & 43 deletions source/sdk/react-native/realm-database/write-transactions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,19 @@
Write Transactions - React Native SDK
=====================================

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
Within a ``<RealmProvider>``, you can access a realm with
the ``useRealm()`` hook. Then, you can create Realm objects
using a :js-sdk:`Realm.write() <Realm.html#write>` transaction block.

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 operations within a write transaction are :wikipedia:`atomic <Atomicity_(database_systems)>`.
If an operation in the write transaction fails, the whole transaction fails,
Realm throws an error, and no changes from the transaction block are applied to the realm.

- 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.

.. important::

Every write operation must occur in a write transaction.

.. example::

Write transactions are callback functions that you pass to a realm
instance. For examples of specific write operations, see
:ref:`react-native-create-objects` or :ref:`react-native-update-objects`.

.. code-block:: javascript

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
})
})
.. literalinclude:: /examples/generated/react-native/ts/create-test.snippet.crud-create-object.tsx
:language: typescript
:emphasize-lines: 6-8

Transaction Lifecycle
---------------------
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit while we're mucking around in here. below, rather than having the note about how transactions work w sync, could you make that either:

  • another bullet point
  • a sub bullet point
  • or just another sentence on this bullet point.

Expand All @@ -61,12 +33,13 @@ After processing a transaction, Realm Database either **commits** it or
Once applied, the realm automatically updates :ref:`live queries
<react-native-live-queries>`. It notifies listeners of created, modified,
and deleted objects.

.. note::

When using :ref:`Sync <sync>`, the SDK also queues
the changes to send to Atlas App Services. The SDK sends
these changes when a network is available.
- When using :ref:`Sync <sync>`, the SDK also queues
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a bullet that after a write transaction, any object or collections returned by useQuery or useObject will rerender for any relevant changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's excellent information to add. Thank you!

the changes to send to Atlas App Services. The SDK sends
these changes when a network is available.

- After a commit, objects and collections returned by ``useQuery`` or
``useObject`` rerender to include relevant changes.

- Realm Database does not apply any operations in a **cancelled**
transaction. Realm Database cancels a transaction if an operation
Expand Down