-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathcrud.txt
135 lines (102 loc) · 4.2 KB
/
crud.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
.. meta::
:robots: noindex, nosnippet
.. _java-read-and-write-data:
===============
CRUD - Java SDK
===============
.. toctree::
:titlesonly:
Create </sdk/java/crud/create>
Read </sdk/java/crud/read>
Update </sdk/java/crud/update>
Delete </sdk/java/crud/delete>
Filter Data </sdk/java/crud/filter-data>
Threading </sdk/java/crud/threading>
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
.. _java-write-operations:
.. _java-realm-database-writes:
.. _java-write-transactions:
Write Operations
----------------
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 Atlas App Services.
- When Realm **cancels** a write transaction or an operation in
the transaction causes an error, all changes are discarded
(or "rolled back").
.. tip:: Realm Object Writes are File Writes
Whenever you create, update, or delete a Realm object,
your changes update the representation of that object in
Realm and emit
:ref:`notifications <java-react-to-changes>` to any subscribed
listeners. As a result, you should only write to Realm
objects when necessary to persist data.
.. include:: /includes/java-synchronous-reads-writes-ui-thread.rst
.. _java-managed-objects:
Managed Objects
---------------
**Managed objects** are live Realm objects that update
based on changes to underlying data in Realm. Managed
objects can only come from an open realm, and receive updates
as long as that realm remains open. Managed objects *cannot be passed
between threads*.
.. _java-unmanaged-objects:
Unmanaged objects
-----------------
**Unmanaged objects** are instances of Realm objects that are
not live. You can get an unmanaged object by manually constructing a
Realm object yourself, or by calling
:java-sdk:`Realm.copyFromRealm() <io/realm/Realm.html#copyFromRealm-E->`.
Unmanaged objects *can be passed between threads*.
.. _java-open-a-transaction:
Run a Transaction
-----------------
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
:java-sdk:`executeTransaction() <io/realm/Realm.html#executeTransaction-io.realm.Realm.Transaction->`
or :java-sdk:`executeTransactionAsync() <io/realm/Realm.html#executeTransactionAsync-io.realm.Realm.Transaction->`.
If the code in the callback throws an exception, Realm
cancels the transaction. Otherwise, Realm commits the
transaction.
.. tabs-realm-languages::
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/WritesTest.snippet.run-a-transaction.kt
:language: kotlin
:copyable: false
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/WritesTest.snippet.run-a-transaction.java
:language: java
:copyable: false