-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathmigrate-from-java-sdk-to-kotlin-sdk.txt
632 lines (456 loc) · 20 KB
/
migrate-from-java-sdk-to-kotlin-sdk.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
.. meta::
:robots: noindex, nosnippet
.. _kotlin-migrate-from-java-sdk-to-kotlin-sdk:
===========================================
Migrate from the Java SDK to the Kotlin SDK
===========================================
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
.. note:: What is the Kotlin SDK?
The Kotlin SDK is a new Realm client SDK built entirely with the
Kotlin programming language. The Kotlin SDK uses an entirely
different codebase from the Java SDK. It is designed specifically
to take advantage of Kotlin language features such as coroutines and
suspend functions. The Java SDK also supports some of these features,
as well as Android applications written in Kotlin. But the
:ref:`Kotlin SDK <kotlin-intro>` is more Kotlin-idiomatic than the
Java SDK.
Overview
--------
The Java SDK and the Kotlin SDK differ in many ways. On this page, you'll
find a high-level comparison of most of the ways the SDKs differ.
Kotlin SDK Architecture
-----------------------
The Java SDK provided live objects, queries, and realms that
automatically update when underlying data changes. The Kotlin SDK still
provides this live interface in write transactions, but otherwise relies
on a new frozen architecture that makes Realm objects easier to work
with. Here are some of the main differences between the Java SDK
architecture and the Kotlin SDK architecture:
- **Frozen by default**: All objects are now frozen. Unlike live objects,
frozen objects do not automatically update after database writes. You
can still access live objects within a write transaction, but passing
a live object out of a write transaction freezes the object.
- **Thread-safety**: All realm instances, objects, query results, and
collections can now be transferred across threads.
- **Singleton**: You now only need one instance of each realm.
:ref:`No need to open and close realms on individual threads.
<kotlin-migration-threading>`
.. seealso::
:ref:`Frozen Architecture in the Kotlin SDK <kotlin-frozen-architecture>`
Opening Realms
--------------
The Java SDK automatically detects Realm Object Models defined in your
application, and uses all of them in the schema of opened realms unless
you specify otherwise. The Kotlin SDK requires you to manually specify
the Realm Object Models to use in your realm schema. Additionally:
- The Kotlin SDK does not provide the ability to set and access a
default realm in your application. Since you can now share realms,
objects, and results across threads, you can rely on a global singleton
instead.
- The Java SDK used :file:`RealmConfiguration.Builder().build()` to
generate instances of :file:`RealmConfiguration`. With the Kotlin SDK,
use the `RealmConfiguration.create()
<{+kotlin-local-prefix+}io.realm.kotlin/-realm-configuration/-companion/create.html>`__
companion method :file:`RealmConfiguration` instead.
- The Java SDK used the static :file:`Realm.getInstance()` method to
open a realm with a given config. With the Kotlin SDK, use the static
:file:`Realm.open()` method instead.
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.open-a-realm.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.open-a-realm.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.open-a-realm.kt
:language: kotlin
:copyable: false
Optionally, use `RealmConfiguration.Builder
<{+kotlin-local-prefix+}io.realm.kotlin/-realm-configuration/-builder/index.html>`__
to customize your configuration even more:
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.open-a-realm-advanced.kt
:language: kotlin
:copyable: false
:caption: Kotlin SDK
.. seealso::
:ref:`Open & Close a Realm <kotlin-open-and-close-a-realm>`
Realm Object Models
-------------------
In the Java SDK, you declare Realm object models in one of two ways:
- extend :file:`RealmObject`
- implement :file:`RealmModel`
The Kotlin SDK uses default methods in the :file:`RealmObject` interface
instead. With the Kotlin SDK, inherit from :file:`RealmObject` to
declare a Realm object model. Annotations work the same way they did
in java for fields with special properties, such as ignored fields,
primary keys, and indexes.
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/Sample.snippet.realm-object-model.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/SampleJava.snippet.realm-object-model.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.realm-object-model.kt
:language: kotlin
:copyable: false
.. seealso::
:ref:`Supported Schemas Types <kotlin-supported-types>`
Relationships
-------------
Both the Java and Kotlin SDKs declare relationships through Realm object
fields:
One-to-One
~~~~~~~~~~
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/Child.snippet.one-to-one-relationship.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/ChildJava.snippet.one-to-one-relationship.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.one-to-one-relationship.kt
:language: kotlin
:copyable: false
One-to-Many
~~~~~~~~~~~
With the Java SDK, you could define one-to-many relationships with fields
of type :file:`RealmList`. The Kotlin SDK still uses fields of
type :file:`RealmList`, but you should instantiate :file:`RealmList`
instances with the `realmListOf()
<{+kotlin-local-prefix+}io.realm.kotlin.ext/realm-list-of.html>`__ companion method.
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/Kid.snippet.one-to-many-relationship.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/KidJava.snippet.one-to-many-relationship.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.one-to-many-relationship.kt
:language: kotlin
:copyable: false
Schema Types
------------
With the Java SDK, you needed to use the :file:`@Required` annotation to
make lists of primitives non-nullable in realm object models. The Kotlin
SDK makes lists of primitives non-nullable by default. Use the
:file:`?` operator to make a list of primitives nullable.
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/CollegeStudent.snippet.schema-types.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/CollegeStudentJava.snippet.schema-types.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.schema-types.kt
:language: kotlin
:copyable: false
Writes
------
The Kotlin SDK introduces new names for the methods that write to realms.
Async
~~~~~
With the Java SDK, you could write asynchronously to a realm with
:file:`realm.executeTransactionAsync()`. The Kotlin SDK uses
the suspend function `realm.write()
<{+kotlin-local-prefix+}io.realm.kotlin/-realm/write.html>`__ instead.
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.write-async.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.write-async.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.write-async.kt
:language: kotlin
:copyable: false
Sync
~~~~
With the Java SDK, you could write synchronously to a realm with
:file:`realm.executeTransaction()`. The Kotlin SDK uses
`realm.writeBlocking()
<{+kotlin-local-prefix+}io.realm.kotlin/-realm/write-blocking.html>`__:
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.write-sync.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.write-sync.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.write-sync.kt
:language: kotlin
:copyable: false
.. seealso::
- :ref:`Create a New Object <kotlin-create-a-new-object>`
- :ref:`Modify an Object <kotlin-modify-an-object>`
- :ref:`Delete an Object <kotlin-delete-an-object>`
Queries
-------
There are several differences between queries in the Java SDK and queries
in the Kotlin SDK:
- With the Java SDK, you can query objects in realms using a fluent
interface or :ref:`Realm Query Language <rql>` (RQL).
The Kotlin SDK only uses RQL.
- The Java SDK uses :file:`realm.where()`
to query realms, whereas the Kotlin SDK uses `realm.query()
<{+kotlin-local-prefix+}io.realm.kotlin.ext/query.html>`__.
- With the Java SDK, you could query asynchronously with
:file:`realmQuery.findAllAsync()` and :file:`realmQuery.findFirstAsync()`.
In the Kotlin SDK, query asynchronously with
`realmQuery.asFlow()
<{+kotlin-local-prefix+}io.realm.kotlin.query/-realm-element-query/as-flow.html>`__.
Once you have a flow of results, you can `collect
<https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/collect.html>`__
the results.
- With the Java SDK, you could query synchronously with
:file:`realmQuery.findAll()` and :file:`realmQuery.findFirst()`.
In the Kotlin SDK, query synchronously with
`realmQuery.find() <{+kotlin-local-prefix+}io.realm.kotlin.query/-realm-element-query/find.html>`__.
Filters
~~~~~~~
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.query-filters.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.query-filters.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.query-filters.kt
:language: kotlin
:copyable: false
Sort, Distinct, Limit
~~~~~~~~~~~~~~~~~~~~~
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.query-sort-limit-distinct.kt
:language: java
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.query-sort-limit-distinct.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.query-sort-limit-distinct.kt
:language: kotlin
:copyable: false
.. seealso::
- :ref:`Filter Data - Kotlin SDK <kotlin-filter-data>`
- :ref:`Sort Queries - Kotlin SDK <kotlin-sort-queries>`
Deletes
-------
In both SDKs, you can only delete live objects. The Kotlin SDK provides
`mutableRealm.findLatest()
<{+kotlin-local-prefix+}io.realm.kotlin/-mutable-realm/find-latest.html>`__
to access a live version of any frozen object. In a write transaction,
you can directly query for live objects and delete them without using
:file:`findLatest()`.
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.deletes.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.deletes.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.deletes.kt
:language: kotlin
:copyable: false
.. seealso::
- :ref:`Delete all Objects of a Type <kotlin-delete-all-objects-of-a-type>`
- :ref:`Delete Multiple Objects <kotlin-delete-multiple-objects>`
- :ref:`Delete an Object <kotlin-delete-an-object>`
Notifications
-------------
In both SDKs, you can subscribe to change to collections of results.
With the Java SDK, you could receive notifications whenever realm results
changed with the following interfaces:
- :file:`realmResults.addChangeListener()`
- RxJava through :file:`asFlowable()`
- Kotlin Extensions with :file:`toFlow()`
The Kotlin SDK replaces all of these options with `realmQuery.asFlow()
<{+kotlin-local-prefix+}io.realm.kotlin.query/-realm-element-query/as-flow.html>`__.
Once you have a flow of results, you can call `collect
<https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/collect.html>`__
to subscribe to changes. Any object of type :file:`UpdatedResults` emitted
by the flow represents a change to the results set.
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.notifications.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.notifications.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.notifications.kt
:language: kotlin
:copyable: false
.. _kotlin-migration-threading:
Threading
---------
With the Java SDK, realms, Realm objects, and results cannot be passed
between threads. The Kotlin SDK freezes these objects by default, making
them thread-safe. Unlike the live objects used by the Java SDK, the
frozen objects found in the Kotlin SDK do not automatically update when
underlying data changes. With the Kotlin SDK, you must use notifications to subscribe to
updates instead.
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.threading.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.threading.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.threading.kt
:language: kotlin
:copyable: false
.. seealso::
:ref:`Frozen Architecture <kotlin-frozen-architecture>`
Migrations
----------
With the Java SDK, migrations were a manual process. The Kotlin SDK
automates migrations, but also gives you access to a similar dynamic
realm interface for custom tweaks to migration logic.
.. list-table::
:header-rows: 0
:widths: 50 50
* - .. tabs::
.. tab:: Java SDK (Kotlin)
:tabid: java-sdk-kotlin
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.migrations.kt
:language: kotlin
:copyable: false
.. tab:: Java SDK (Java)
:tabid: java-sdk-java
.. literalinclude:: /examples/generated/java/local/MigrateFromJavaToKotlinSDKTest.snippet.migrations.java
:language: java
:copyable: false
- .. tabs::
.. tab:: Kotlin SDK
:tabid: kotlin-sdk
.. literalinclude:: /examples/generated/kotlin/MigrateFromJavaToKotlinSDKTest.snippet.migrations.kt
:language: kotlin
:copyable: false
What Next?
----------
Now that you understand the differences between the Java SDK and the
Kotlin SDK, check out the rest of the :ref:`Kotlin SDK documentation
<kotlin-intro>`.