|
| 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. |
0 commit comments