5
5
Write Transactions - React Native SDK
6
6
=====================================
7
7
8
- .. contents:: On this page
9
- :local:
10
- :backlinks: none
11
- :depth: 2
12
- :class: singlecol
8
+ Within a ``<RealmProvider>``, you can access a realm with
9
+ the ``useRealm()`` hook. Then, you can create Realm objects
10
+ using a :js-sdk:`Realm.write() <Realm.html#write>` transaction block.
13
11
14
- A write transaction is a function that modifies objects in a realm. Write
15
- transactions let you create, modify, or delete Realm objects. They handle
16
- operations in a single, idempotent update. A transaction is
17
- *all or nothing*. Either:
12
+ All operations within a write transaction are :wikipedia:`atomic <Atomicity_(database_systems)>`.
13
+ If an operation in the write transaction fails, the whole transaction fails,
14
+ Realm throws an error, and no changes from the transaction block are applied to the realm.
18
15
19
- - All the operations in the transaction succeed, or;
20
- - If any operation fails, none of the operations complete.
16
+ Every write operation must occur in a write transaction.
21
17
22
- .. important::
23
-
24
- Every write operation must occur in a write transaction.
25
-
26
- .. example::
27
-
28
- Write transactions are callback functions that you pass to a realm
29
- instance. For examples of specific write operations, see
30
- :ref:`react-native-create-objects` or :ref:`react-native-update-objects`.
31
-
32
- .. code-block:: javascript
33
-
34
- const realm = await Realm.open();
35
- realm.write(() => {
36
- const tesla = realm.create("Car", {
37
- make: "Tesla",
38
- model: "Model S",
39
- year: 2020,
40
- miles: 12000
41
- })
42
- const honda = realm.create("Car", {
43
- make: "Honda",
44
- model: "Civic",
45
- year: 2018,
46
- miles: 30000
47
- })
48
- })
18
+ .. literalinclude:: /examples/generated/react-native/ts/create-test.snippet.crud-create-object.tsx
19
+ :language: typescript
20
+ :emphasize-lines: 6-8
49
21
50
22
Transaction Lifecycle
51
23
---------------------
@@ -61,12 +33,13 @@ After processing a transaction, Realm Database either **commits** it or
61
33
Once applied, the realm automatically updates :ref:`live queries
62
34
<react-native-live-queries>`. It notifies listeners of created, modified,
63
35
and deleted objects.
64
-
65
- .. note::
66
36
67
- When using :ref:`Sync <sync>`, the SDK also queues
68
- the changes to send to Atlas App Services. The SDK sends
69
- these changes when a network is available.
37
+ - When using :ref:`Sync <sync>`, the SDK also queues
38
+ the changes to send to Atlas App Services. The SDK sends
39
+ these changes when a network is available.
40
+
41
+ - After a commit, objects and collections returned by ``useQuery`` or
42
+ ``useObject`` rerender to include relevant changes.
70
43
71
44
- Realm Database does not apply any operations in a **cancelled**
72
45
transaction. Realm Database cancels a transaction if an operation
0 commit comments