-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathquick-start-sync.txt
362 lines (248 loc) · 9.75 KB
/
quick-start-sync.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
.. meta::
:robots: noindex, nosnippet
.. _java-client-quick-start-sync:
================================
Quick Start with Sync - Java SDK
================================
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
.. tip:: This Guide uses Device Sync
This guide helps you get started with an Android application that
communicates with an App backend. The App provides features
like :ref:`Sync <sync>`, Realm Functions, and user management. If
your application requires only local database functionality,
check out the :ref:`Quick Start (Local-only)
<java-client-quick-start-local>` guide.
This page contains information to quickly get Atlas App Services
integrated into your app. Before you begin, ensure you have:
- :ref:`Created an App <create-a-realm-app>`
- :ref:`Enabled Sync <enable-sync>`
- :ref:`Installed the Java SDK <java-install>`
.. include:: /includes/java-initialize-realm.rst
.. _java-quick-start-init-app:
Initialize the App
------------------
.. include:: /includes/access-app-id.rst
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.initialize-the-app.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.initialize-the-app.kt
:language: kotlin
:copyable: false
.. note:: Android Studio Errors?
If Android Studio does not recognize the ``Realm``, ``App``, or
``AppConfiguration`` types, there could be a problem with the
your Gradle build configuration. To fix the issue:
- Clean your project with ``Build > Clean Project``
- Rebuild your project based on your updated ``build.gradle`` file
with ``Build > Rebuild Project``
- Revisit the :ref:`Install the Java SDK <java-install>`
guide to make sure that you installed the dependencies correctly.
Define Your Object Model
------------------------
Your application's **data model** defines the structure of data
stored within Realm and synchronized to and from
App Services. You can define your application's data model in two ways:
- Via :ref:`schemas <schemas>` in App Services.
- Via Kotlin or Java classes in your application code with
:ref:`Realm Object Models <create-schema-from-rom>`.
This quick start uses the latter approach, which defines your schema
using classes in your mobile application code. To define your App's
object model in this way, you need to enable :ref:`Development Mode
<development-mode>`.
Once you've enabled Development Mode, add the following class
definitions to your application code:
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/Task.snippet.define-object-model.java
:language: java
:copyable: false
.. literalinclude:: /examples/generated/java/sync/TaskStatus.snippet.complete.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.define-object-model.kt
:language: kotlin
:copyable: false
.. _java-quick-start-authenticate:
Authenticate a User
-------------------
When you have enabled :ref:`anonymous authentication <anonymous-authentication>` in the
App Services UI, users can immediately log into your app without providing any identifying
information:
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.authenticate-a-user.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.authenticate-a-user.kt
:language: kotlin
:copyable: false
Realm provides many additional ways to authenticate, register, and link users.
Open a Realm
------------
Once you have :ref:`enabled Sync <enable-sync>` and authenticated a
user, you can open a synced :ref:`realm <java-realms>`. Use
``SyncConfiguration`` to control the specifics of how your application
synchronizes data with App Services, including timeouts, synchronous
reads and writes on the UI thread, and more.
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.open-a-realm.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.open-a-realm.kt
:language: kotlin
:copyable: false
Create, Read, Update, and Delete Objects
----------------------------------------
Once you have opened a realm, you can modify the
:ref:`objects <java-realm-objects>` within that realm in a
:ref:`write transaction <java-open-a-transaction>` block.
.. include:: /includes/java-synchronous-reads-writes-ui-thread.rst
To create a new ``Task``, instantiate an instance of the
``Task`` class and add it to the realm in a write block:
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.create-object.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.create-object.kt
:language: kotlin
:copyable: false
You can retrieve a live :ref:`collection <java-client-collections>`
of all items in the realm:
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.read-object.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.read-object.kt
:language: kotlin
:copyable: false
You can also filter that collection using a :ref:`filter
<java-client-query-engine>`:
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.filter-collection.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.filter-collection.kt
:language: kotlin
:copyable: false
To modify a task, update its properties in a write transaction block:
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.update-object.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.update-object.kt
:language: kotlin
:copyable: false
Finally, you can delete a task by calling the ``deleteFromRealm()``
method in a write transaction block:
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.delete-object.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.delete-object.kt
:language: kotlin
:copyable: false
Watch for Changes
-----------------
You can :ref:`watch a realm, collection, or object for changes
<java-client-notifications>` by attaching a custom
``OrderedRealmCollectionChangeListener`` with the ``addChangeListener()``
method:
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.watch-for-changes.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.watch-for-changes.kt
:language: kotlin
:copyable: false
Log Out
-------
Once logged in, you can log out:
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.log-out.java
:language: java
:copyable: false
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.log-out.kt
:language: kotlin
:copyable: false
Complete Example
----------------
Run the complete example by replacing the appId with your realm app ID.
If you're running this project in a fresh Android Studio project, you can
copy and paste this file into your application's ``MainActivity`` -- just
remember to:
- change the package declaration so it matches your project
- replace the App ID placeholder with your App's App ID
- update the ``import`` statements for ``Task`` and ``TaskStatus`` if
you're using Java
.. tabs-realm-languages::
.. tab::
:tabid: java
.. literalinclude:: /examples/generated/java/sync/Task.snippet.define-object-model.java
:caption: Task.java
:language: java
.. literalinclude:: /examples/generated/java/sync/TaskStatus.snippet.complete.java
:caption: TaskStatus.java
:language: java
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.complete.java
:caption: MainActivity.java
:language: java
.. tab::
:tabid: kotlin
.. literalinclude:: /examples/generated/java/sync/MainActivity.snippet.complete.kt
:caption: MainActivity.kt
:language: kotlin
Output
------
Running the above code should produce output resembling the following:
.. code-block:: shell
Successfully authenticated anonymously.
Updated range: 0 to 1
Deleted range: 0 to 1
Successfully logged out.