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