Skip to content

Commit 1106b57

Browse files
authored
Syncdata (#549)
SyncData
1 parent ffc7c0e commit 1106b57

File tree

5 files changed

+102
-24
lines changed

5 files changed

+102
-24
lines changed

source/dotnet/open-a-realm.txt

+3-24
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,12 @@ Open and Close a Realm
1212
:depth: 2
1313
:class: singlecol
1414

15+
.. _dotnet-open-synced-realm:
16+
1517
Open a Synced Realm
1618
-------------------
17-
To open a synced {+realm+}, call the
18-
:dotnet-sdk:`GetInstanceAsync() <reference/Realms.Realm.html#Realms_Realm_GetInstanceAsync_Realms_RealmConfigurationBase_System_Threading_CancellationToken_>`
19-
method, passing in a
20-
:dotnet-sdk:`SyncConfiguration <reference/Realms.Sync.SyncConfiguration.html>`
21-
object that includes the partition name and
22-
the user. The following code demonstrates this:
23-
24-
.. literalinclude:: /examples/generated/code/start/Examples.codeblock.open-synced-realm.cs
25-
:language: csharp
26-
27-
In the above example, the code shows how to open the {+realm+} *asynchronously*
28-
by calling ``GetInstanceAsync()``. You can also open a {+realm+} *synchronously*
29-
by calling the
30-
:dotnet-sdk:`GetInstance() <reference/Realms.Realm.html#Realms_Realm_GetInstance_System_String_>`
31-
method, which is necessary if the device is offline.
32-
33-
.. literalinclude:: /examples/generated/code/start/Examples.codeblock.open-synced-realm-sync.cs
34-
:language: csharp
35-
36-
.. note::
3719

38-
The first time a user logs on to your realm app, you should open the realm
39-
*asynchronously* to sync data from the server to the device. After that initial
40-
connection, you can open a realm *synchronously* to ensure the app works in
41-
an offline state.
20+
.. include:: /includes/dotnet-open-synced-realm.rst
4221

4322
Open a Local (Non-Synced) Realm
4423
-------------------------------

source/dotnet/quick-start.txt

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ attribute so we can use .NET-friendly casing on our property names.
8080
.. literalinclude:: /examples/generated/code/final/RealmTask.cs
8181
:language: csharp
8282

83+
.. _dotnet-quick-start-authenticate:
84+
8385
Authenticate a User
8486
-------------------
8587

source/dotnet/sync-data.txt

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
.. _dotnet-sync-data:
2+
3+
=========
4+
Sync Data
5+
=========
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
.. include:: /includes/sync-beta-note.rst
16+
17+
Prerequisites
18+
-------------
19+
20+
Before you can access a synced {+realm+} from the client, you must:
21+
22+
- :ref:`Enable sync <enable-sync>` in the {+ui+}.
23+
24+
- :ref:`Authenticate a user <dotnet-quick-start-authenticate>` in
25+
your client project.
26+
27+
Open a Synced Realm
28+
-------------------
29+
30+
.. include:: /includes/dotnet-open-synced-realm.rst
31+
32+
The :ref:`partition value <partitioning>` specifies which subset of your data to sync.
33+
This is typically a user ID, project ID, store ID, or some other category identifier in
34+
your app that has particular relevance to the current user.
35+
36+
.. seealso:: :ref:`Partition Atlas Data into Realms <partitioning>`
37+
38+
39+
Sync Data
40+
---------
41+
42+
The syntax to :ref:`read <dotnet-realm-database-reads>` and :ref:`write
43+
<dotnet-realm-database-writes>` on a synced {+realm+} is identical to the syntax
44+
for non-synced {+realms+}. While you work with local data, a background thread
45+
efficiently integrates, uploads, and downloads changesets.
46+
47+
.. admonition:: When Using Sync, Avoid Writes on the Main Thread
48+
:class: important
49+
50+
The fact that {+service-short+} performs sync integrations on a background thread means
51+
that if you write to your {+realm+} on the main thread, there's a small chance your UI
52+
could appear to hang as it waits for the background sync thread to finish a write
53+
transaction. Therefore, it's a best practice :ref:`never to write on the main thread
54+
when using {+sync+} <dotnet-threading-three-rules>`.
55+
56+
The following code creates a new ``Task`` object and writes it to the {+realm+}:
57+
58+
.. literalinclude:: /examples/generated/code/start/Examples.codeblock.create.cs
59+
:language: csharp
60+
61+
.. seealso:: :ref:`Threading <dotnet-client-threading>`
62+
63+
Summary
64+
-------
65+
66+
- Open a synced {+realm+} with the configuration object generated when you pass
67+
a :term:`partition value` to the user object's ``SyncConfiguration`` Builder.
68+
- Compared to using a non-synced {+realm+}, there is no special syntax for reading
69+
from, writing to, or observing objects on a synced {+realm+}.
70+
- You should avoid writing to a synced {+realm+} on the main thread.

source/dotnet/threading.txt

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ maintainable multithreaded code that avoids issues like
3131
deadlocking and race conditions. {+service+} aims to
3232
simplify this for you.
3333

34+
.. _dotnet-threading-three-rules:
35+
3436
Three Rules to Keep in Mind
3537
~~~~~~~~~~~~~~~~~~~~~~~~~~~
3638

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
To open a synced {+realm+}, call the
2+
:dotnet-sdk:`GetInstanceAsync() <reference/Realms.Realm.html#Realms_Realm_GetInstanceAsync_Realms_RealmConfigurationBase_System_Threading_CancellationToken_>`
3+
method, passing in a
4+
:dotnet-sdk:`SyncConfiguration <reference/Realms.Sync.SyncConfiguration.html>`
5+
object that includes the partition name and
6+
the user. The following code demonstrates this:
7+
8+
.. literalinclude:: /examples/generated/code/start/Examples.codeblock.open-synced-realm.cs
9+
:language: csharp
10+
11+
In the above example, the code shows how to open the {+realm+} *asynchronously*
12+
by calling ``GetInstanceAsync()``. You can also open a {+realm+} *synchronously*
13+
by calling the
14+
:dotnet-sdk:`GetInstance() <reference/Realms.Realm.html#Realms_Realm_GetInstance_System_String_>`
15+
method, which is necessary if the device is offline.
16+
17+
.. literalinclude:: /examples/generated/code/start/Examples.codeblock.open-synced-realm-sync.cs
18+
:language: csharp
19+
20+
.. note::
21+
22+
The first time a user logs on to your realm app, you should open the realm
23+
*asynchronously* to sync data from the server to the device. After that initial
24+
connection, you can open a realm *synchronously* to ensure the app works in
25+
an offline state.

0 commit comments

Comments
 (0)