• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16Avoid using **unifiedDataChannel** APIs in multi-threaded calls.
17
18The 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:
19
20+ **udmf**: protocol used to provide the data channel.
21
22+ *intention*: an enum of the data channel types supported by the UDMF.
23
24+ *bundleName*: bundle name of the data source application.
25
26+ *groupId*: group ID used for batch data management.
27
28Currently, the UDMF provides the public data channel for cross-application data sharing.
29
30The 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**.
31
32## Available APIs
33
34The 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).
35
36| API                                                                                   | Description                                         |
37|-----------------------------------------------------------------------------------------|---------------------------------------------|
38| insertData(options: Options, data: UnifiedData, callback: AsyncCallback\<string>): void | Inserts data to the UDMF public data channel. A unique data identifier is returned.|
39| updateData(options: Options, data: UnifiedData, callback: AsyncCallback\<void>): void   | Updates the data in the UDMF public data channel.          |
40| queryData(options: Options, callback: AsyncCallback\<Array\<UnifiedData>>): void        | Queries data in the UDMF public data channel.              |
41| deleteData(options: Options, callback: AsyncCallback\<Array\<UnifiedData>>): void       | Deletes data from the UDMF public data channel. The deleted data set is returned.|
42
43
44## How to Develop
45
46The 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.
47
48### Data Provider
49
501. Import the **unifiedDataChannel** and **uniformTypeDescriptor** modules.
51
52   ```ts
53   import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData';
54   ```
552. Create a **UnifiedData** object and insert it into the UDMF public data channel.
56
57   ```ts
58   import { BusinessError } from '@kit.BasicServicesKit';
59   let plainText = new unifiedDataChannel.PlainText();
60   plainText.textContent = 'hello world!';
61   let unifiedData = new unifiedDataChannel.UnifiedData(plainText);
62
63   // Specify the type of the data channel to which the data is to be inserted.
64   let options: unifiedDataChannel.Options = {
65     intention: unifiedDataChannel.Intention.DATA_HUB
66   }
67   try {
68     unifiedDataChannel.insertData(options, unifiedData, (err, key) => {
69       if (err === undefined) {
70         console.info(`Succeeded in inserting data. key = ${key}`);
71       } else {
72         console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `);
73       }
74     });
75   } catch (e) {
76     let error: BusinessError = e as BusinessError;
77     console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `);
78   }
79   ```
803. Update the **UnifiedData** object inserted.
81
82   ```ts
83   let plainTextUpdate = new unifiedDataChannel.PlainText();
84   plainTextUpdate.textContent = 'How are you!';
85   let unifiedDataUpdate = new unifiedDataChannel.UnifiedData(plainTextUpdate);
86
87   // Specify the URI of the UnifiedData object to update.
88   let optionsUpdate: unifiedDataChannel.Options = {
89     //The key here is an example and cannot be directly used. Use the value in the callback of insertData().
90     key: 'udmf://DataHub/com.ohos.test/0123456789'
91   };
92
93   try {
94     unifiedDataChannel.updateData(optionsUpdate, unifiedDataUpdate, (err) => {
95       if (err === undefined) {
96         console.info('Succeeded in updating data.');
97       } else {
98         console.error(`Failed to update data. code is ${err.code},message is ${err.message} `);
99       }
100     });
101   } catch (e) {
102     let error: BusinessError = e as BusinessError;
103     console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `);
104   }
105   ```
1064. Delete the **UnifiedData** object from the UDMF public data channel.
107
108   ```ts
109   // Specify the type of the data channel whose data is to be deleted.
110   let optionsDelete: unifiedDataChannel.Options = {
111     intention: unifiedDataChannel.Intention.DATA_HUB
112   };
113
114   try {
115     unifiedDataChannel.deleteData(optionsDelete, (err, data) => {
116       if (err === undefined) {
117         console.info(`Succeeded in deleting data. size = ${data.length}`);
118         for (let i = 0; i < data.length; i++) {
119           let records = data[i].getRecords();
120           for (let j = 0; j < records.length; j++) {
121             if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
122               let text = records[j] as unifiedDataChannel.PlainText;
123               console.info(`${i + 1}.${text.textContent}`);
124             }
125           }
126         }
127       } else {
128         console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `);
129       }
130     });
131   } catch (e) {
132     let error: BusinessError = e as BusinessError;
133     console.error(`Delete data throws an exception. code is ${error.code},message is ${error.message} `);
134   }
135   ```
136
137### Data Consumer
138
1391. Import the **unifiedDataChannel** and **uniformTypeDescriptor** modules.
140
141   ```ts
142   import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData';
143   ```
1442. Query the full data in the UDMF public data channel.
145
146   ```ts
147   import { BusinessError } from '@kit.BasicServicesKit';
148   // Specify the type of the data channel whose data is to be queried.
149   let options: unifiedDataChannel.Options = {
150     intention: unifiedDataChannel.Intention.DATA_HUB
151   };
152
153   try {
154     unifiedDataChannel.queryData(options, (err, data) => {
155       if (err === undefined) {
156         console.info(`Succeeded in querying data. size = ${data.length}`);
157         for (let i = 0; i < data.length; i++) {
158           let records = data[i].getRecords();
159           for (let j = 0; j < records.length; j++) {
160             if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
161               let text = records[j] as unifiedDataChannel.PlainText;
162               console.info(`${i + 1}.${text.textContent}`);
163             }
164           }
165         }
166       } else {
167         console.error(`Failed to query data. code is ${err.code},message is ${err.message} `);
168       }
169     });
170   } catch(e) {
171     let error: BusinessError = e as BusinessError;
172     console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `);
173   }
174   ```
175