# Distributed Data Service Development<a name="EN-US_TOPIC_0000001183227606"></a>
# Distributed Data Service Development
## When to Use<a name="section29812823611"></a>
## When to Use
The DDS implements synchronization of application data across user devices. When data is added, deleted, or modified for an application on a device, the same application on another device can obtain the data changes. The DDS applies to the distributed gallery, messages, contacts, and file manager.
The Distributed Data Service (DDS) implements synchronization of application data across user devices. When data is added, deleted, or modified for an application on a device, the same application on another device can obtain the updated data. The DDS applies to the distributed gallery, messages, Contacts, and file manager.
## Available APIs<a name="section10795222103613"></a>
The following table describes the APIs provided by the OpenHarmony DDS module.
## Available APIs
The table below describes the APIs provided by the OpenHarmony DDS module.
| Creating a distributed database | createKVManager(config: KVManagerConfig, callback: AsyncCallback<KVManager>): void<br>createKVManager(config: KVManagerConfig): Promise<KVManager> | Creates a **KVManager** object for database management.|
| Creating a distributed KV store | getKVStore<T extends KVStore>(storeId: string, options: Options, callback: AsyncCallback<T>): void<br>getKVStore<T extends KVStore>(storeId: string, options: Options): Promise<T> | Obtains the KV store with the specified **Options** and **storeId**.|
</th>
| Managing data in a distributed KV store| put(key: string, value: Uint8Array \| string \| number \| boolean, callback: AsyncCallback<void>): void<br>put(key: string, value: Uint8Array \| string \| number \| boolean): Promise<void> | Inserts and updates data. |
| Managing data in a distributed KV store| delete(key: string, callback: AsyncCallback<void>): void<br>delete(key: string): Promise<void> | Deletes data. |
</th>
| Managing data in a distributed KV store| get(key: string, callback: AsyncCallback<Uint8Array \| string \| boolean \| number>): void<br>get(key: string): Promise<Uint8Array \| string \| boolean \| number> | Queries data. |
</tr>
| Subscribing to changes in the distributed data | on(event: 'dataChange', type: SubscribeType, observer: Callback<ChangeNotification>): void<br>on(event: 'syncComplete', syncCallback: Callback<Array<[string, number]>>): void | Subscribes to data changes in the KV store. |
</thead>
| Synchronize data across devices. | sync(deviceIdList: string[], mode: SyncMode, allowedDelayMs?: number): void | Triggers database synchronization in manual mode. |
<tbody><trid="row14541456191517"><tdclass="cellrowborder"rowspan="2"valign="top"width="13.171317131713172%"headers="mcps1.2.4.1.1 "><pid="p65410566158"><aname="p65410566158"></a><aname="p65410566158"></a>Creating a distributed database</p>
<tdclass="cellrowborder"valign="top"width="33.3033303330333%"headers="mcps1.2.4.1.3 "><pid="p101014641817"><aname="p101014641817"></a><aname="p101014641817"></a>Creates a <strongid="b1522993164717"><aname="b1522993164717"></a><aname="b1522993164717"></a>KVManager</strong> object for database management.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1939220263198"><aname="p1939220263198"></a><aname="p1939220263198"></a>Obtains the KV store with specified <strongid="b1047044512494"><aname="b1047044512494"></a><aname="b1047044512494"></a>Options</strong> and <strongid="b76466490494"><aname="b76466490494"></a><aname="b76466490494"></a>storeId</strong>.</p>
</td>
</tr>
<trid="row157982545817"><tdclass="cellrowborder"rowspan="3"valign="top"width="13.171317131713172%"headers="mcps1.2.4.1.1 "><pid="p37985541683"><aname="p37985541683"></a><aname="p37985541683"></a>Managing data in a distributed database</p>
<tdclass="cellrowborder"valign="top"width="33.3033303330333%"headers="mcps1.2.4.1.3 "><pid="p176461135185113"><aname="p176461135185113"></a><aname="p176461135185113"></a>Inserts and updates data.</p>
<trid="row79301383912"><tdclass="cellrowborder"valign="top"width="13.171317131713172%"headers="mcps1.2.4.1.1 "><pid="p9931153113918"><aname="p9931153113918"></a><aname="p9931153113918"></a>Subscribing to changes in the distributed data</p>
<tdclass="cellrowborder"valign="top"width="33.3033303330333%"headers="mcps1.2.4.1.3 "><pid="p1793120318397"><aname="p1793120318397"></a><aname="p1793120318397"></a>Subscribes to data changes in the database.</p>
<tdclass="cellrowborder"valign="top"width="33.3033303330333%"headers="mcps1.2.4.1.3 "><pid="p1319561811538"><aname="p1319561811538"></a><aname="p1319561811538"></a>Triggers database synchronization in manual mode.</p>
</td>
</tr>
</tbody>
</table>
## How to Develop<a name="section1252143014159"></a>
The following uses a single KV store as an example to describe the development procedure.
1. Import the distributed database module.
## How to Develop
The following uses a single KV store as an example to describe the development procedure.
1. Import the distributed data module.
```
```
import distributedData from '@ohos.data.distributedData';
import distributedData from '@ohos.data.distributedData';
```
```
2. Create a **KvManager** instance based on the specified **KvManagerConfig** object.
2. Create a **KvManager** instance based on the specified **KvManagerConfig** object.
1. Create a **KvManagerConfig** object based on the application context.
1. Create a **KvManagerConfig** object based on the application context.
2. Create a **KvManager** instance.
2. Create a **KvManager** instance.
The sample code is as follows:
The sample code is as follows:
```
```
let kvManager;
let kvManager;
try {
try {
...
@@ -112,12 +62,10 @@ The following uses a single KV store as an example to describe the development p
...
@@ -112,12 +62,10 @@ The following uses a single KV store as an example to describe the development p
```
```
3. Create and obtain a single KV store.
3. Create and obtain a single KV store.
1. Declare the ID of the single KV store to create.
1. Declare the ID of the single KV store to create.
2. Create a single KV store. You are advised to disable automatic synchronization \(**autoSync:false**\) and call **sync** if a synchronization is required.
2. Create a single KV store. You are advised to disable automatic synchronization (**autoSync:false**) and call **sync** if a synchronization is required.
The sample code is as follows:
The sample code is as follows:
```
```
let kvStore;
let kvStore;
try {
try {
...
@@ -127,7 +75,7 @@ The following uses a single KV store as an example to describe the development p
...
@@ -127,7 +75,7 @@ The following uses a single KV store as an example to describe the development p
>For data synchronization between networked devices, you are advised to open the distributed database during application startup to obtain the database handle. With this database handle \(**kvStore** in this example\), you can perform operations, such as inserting data into the database, without creating the database repeatedly during the lifecycle of the handle.
> For data synchronization between networked devices, you are advised to open the distributed KV store during application startup to obtain the database handle. With this database handle (**kvStore** in this example), you can perform operations, such as inserting data into the KV store, without creating the KV store repeatedly during the lifecycle of the handle.
4. Subscribe to changes in the distributed data.
4. Subscribe to changes in the distributed data.
The following is the sample code for subscribing to the data changes of a single KV store:
The following is the sample code for subscribing to the data changes of a single KV store:
```
```
kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_ALL, function (data) {
kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_ALL, function (data) {
@@ -205,31 +148,34 @@ The following uses a single KV store as an example to describe the development p
...
@@ -205,31 +148,34 @@ The following uses a single KV store as an example to describe the development p
```
```
7. Synchronize data to other devices.
7. Synchronize data to other devices.
Select the devices in the same network and the synchronization mode to synchronize data.
Select the devices in the same network and the synchronization mode to synchronize data.
The following is the sample code for data synchronization in a single KV store. **deviceIds** can be obtained by deviceManager by calling **getTrustedDeviceListSync\(\)**, and **1000** indicates that the maximum delay time is 1000 ms.
The following is the sample code for data synchronization in a single KV store. **deviceIds** can be obtained by deviceManager by calling **getTrustedDeviceListSync()**, and **1000** indicates that the maximum delay time is 1000 ms.
```
```
import deviceManager from '@ohos.distributedHardware.deviceManager';
import deviceManager from '@ohos.distributedHardware.deviceManager';