1# Sharing Data via Unified Data Channels
2
3
4## When to Use
5
6In many-to-many data sharing across applications, a data channel needs to be provided to access data of different applications and share the data with other applications.
7
8The Unified Data Management Framework (UDMF) provides unified data channels and standard data access interfaces for different service scenarios of many-to-many cross-application data sharing.
9
10## Definition and Implementation of Unified Data Channels
11
12The unified data channel provides cross-application data access for various service scenarios. It can temporarily store the unified data objects to be shared by an application, and manage the data modification and deletion permissions and lifecycle of the data according to certain policies.
13
14The unified data channel is implemented by the system ability provided by the UDMF. When an application (data provider) needs to share data, it calls the **insertData()** method provided by the UDMF to write the data to the UDMF data channel, and calls UDMF **updateData()** or **deleteData()** to update or delete the data saved by the application. The target application (data consumer) can access the data by the APIs provided by the UDMF. The UDMF manages the data lifecycle in a unified manner and deletes the data that has been stored for more than one hour every hour.
15
16The unified data object (**UnifiedData**) is uniquely identified by a URI in the UDMF data channel. The URI is in the **udmf://*intention*/*bundleName*/*groupId*** format, where:
17
18+ **udmf**: protocol used to provide the data channel.
19
20+ *intention*: an enum of the data channel types supported by the UDMF.
21
22+ *bundleName*: bundle name of the data source application.
23
24+ *groupId*: group ID used for batch data management.
25
26Currently, the UDMF provides the public data channel for cross-application data sharing.
27
28The public data channel is a data channel shared by applications. All applications can write data to the channel. The data writer can update, delete, and query data based on the unique identifier generated when the data is written. The data reader can read only the full data in the data channel. The intention type of the public data channel is **DATA_HUB**.
29
30## Available APIs
31
32The following table lists the UDMF APIs. All of them are executed asynchronously in callback or promise mode. In the following table, callback-based APIs are used as an example. For details about more APIs and their usage, see [Unified Data Channel](../reference/apis-arkdata/js-apis-data-unifiedDataChannel.md) and [Standard Data Definition and Description](../reference/apis-arkdata/js-apis-data-uniformTypeDescriptor.md).
33
34| API                                                                                   | Description                                         |
35|-----------------------------------------------------------------------------------------|---------------------------------------------|
36| insertData(options: Options, data: UnifiedData, callback: AsyncCallback\<string>): void | Inserts data to the UDMF public data channel. A unique data identifier is returned.|
37| updateData(options: Options, data: UnifiedData, callback: AsyncCallback\<void>): void   | Updates the data in the UDMF public data channel.          |
38| queryData(options: Options, callback: AsyncCallback\<Array\<UnifiedData>>): void        | Queries data in the UDMF public data channel.              |
39| deleteData(options: Options, callback: AsyncCallback\<Array\<UnifiedData>>): void       | Deletes data from the UDMF public data channel. The deleted data set is returned.|
40
41
42## How to Develop
43
44The following example describes how to implement many-to-many data sharing. The data provider calls **insertData()** provided by the UMDF to write data to the public data channel. The return value (unique identifier of the data written) can be used to update or delete the data. The data consumer uses the query() APIs provided by the UDMF to obtain full data of the public data channel.
45
46### Data Provider
47
481. Import the **unifiedDataChannel** and **uniformTypeDescriptor** modules.
49
50   ```ts
51   import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData';
52   ```
532. Create a **UnifiedData** object and insert it into the UDMF public data channel.
54
55   ```ts
56   import { BusinessError } from '@kit.BasicServicesKit';
57   let plainText = new unifiedDataChannel.PlainText();
58   plainText.textContent = 'hello world!';
59   let unifiedData = new unifiedDataChannel.UnifiedData(plainText);
60
61   // Specify the type of the data channel to which the data is to be inserted.
62   let options: unifiedDataChannel.Options = {
63     intention: unifiedDataChannel.Intention.DATA_HUB
64   }
65   try {
66     unifiedDataChannel.insertData(options, unifiedData, (err, key) => {
67       if (err === undefined) {
68         console.info(`Succeeded in inserting data. key = ${key}`);
69       } else {
70         console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `);
71       }
72     });
73   } catch (e) {
74     let error: BusinessError = e as BusinessError;
75     console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `);
76   }
77   ```
783. Update the **UnifiedData** object inserted.
79
80   ```ts
81   let plainTextUpdate = new unifiedDataChannel.PlainText();
82   plainTextUpdate.textContent = 'How are you!';
83   let unifiedDataUpdate = new unifiedDataChannel.UnifiedData(plainTextUpdate);
84
85   // Specify the URI of the UnifiedData object to update.
86   let optionsUpdate: unifiedDataChannel.Options = {
87     //The key here is an example and cannot be directly used. Use the value in the callback of insertData().
88     key: 'udmf://DataHub/com.ohos.test/0123456789'
89   };
90
91   try {
92     unifiedDataChannel.updateData(optionsUpdate, unifiedDataUpdate, (err) => {
93       if (err === undefined) {
94         console.info('Succeeded in updating data.');
95       } else {
96         console.error(`Failed to update data. code is ${err.code},message is ${err.message} `);
97       }
98     });
99   } catch (e) {
100     let error: BusinessError = e as BusinessError;
101     console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `);
102   }
103   ```
1044. Delete the **UnifiedData** object from the UDMF public data channel.
105
106   ```ts
107   // Specify the type of the data channel whose data is to be deleted.
108   let optionsDelete: unifiedDataChannel.Options = {
109     intention: unifiedDataChannel.Intention.DATA_HUB
110   };
111
112   try {
113     unifiedDataChannel.deleteData(optionsDelete, (err, data) => {
114       if (err === undefined) {
115         console.info(`Succeeded in deleting data. size = ${data.length}`);
116         for (let i = 0; i < data.length; i++) {
117           let records = data[i].getRecords();
118           for (let j = 0; j < records.length; j++) {
119             if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
120               let text = records[j] as unifiedDataChannel.PlainText;
121               console.info(`${i + 1}.${text.textContent}`);
122             }
123           }
124         }
125       } else {
126         console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `);
127       }
128     });
129   } catch (e) {
130     let error: BusinessError = e as BusinessError;
131     console.error(`Delete data throws an exception. code is ${error.code},message is ${error.message} `);
132   }
133   ```
134
135### Data Consumer
136
1371. Import the **unifiedDataChannel** and **uniformTypeDescriptor** modules.
138
139   ```ts
140   import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData';
141   ```
1422. Query the full data in the UDMF public data channel.
143
144   ```ts
145   import { BusinessError } from '@kit.BasicServicesKit';
146   // Specify the type of the data channel whose data is to be queried.
147   let options: unifiedDataChannel.Options = {
148     intention: unifiedDataChannel.Intention.DATA_HUB
149   };
150
151   try {
152     unifiedDataChannel.queryData(options, (err, data) => {
153       if (err === undefined) {
154         console.info(`Succeeded in querying data. size = ${data.length}`);
155         for (let i = 0; i < data.length; i++) {
156           let records = data[i].getRecords();
157           for (let j = 0; j < records.length; j++) {
158             if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
159               let text = records[j] as unifiedDataChannel.PlainText;
160               console.info(`${i + 1}.${text.textContent}`);
161             }
162           }
163         }
164       } else {
165         console.error(`Failed to query data. code is ${err.code},message is ${err.message} `);
166       }
167     });
168   } catch(e) {
169     let error: BusinessError = e as BusinessError;
170     console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `);
171   }
172   ```
173