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 access 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 **insert()** method provided by the UDMF to write the data to the UDMF data channel, and calls UDMF **update()** or **delete()** to update or delete the data. After passing the permission verification, the target application (data consumer) calls the UDMF **read()** to access the data. After the data is read, the UDMF performs lifecycle management of the data. 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 28**Public data channel**: allows applications to write and read data. The corresponding **intention** 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 writes data to the UMDF public data channel, and updates and deletes the data. The data consumer obtains the data shared by the data provider. 45 46### Data Provider 47 481. Import the **@ohos.data.unifiedDataChannel** and **@ohos.data.uniformTypeDescriptor** modules. 49 50 ```ts 51 import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 52 import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor'; 53 ``` 542. Create a **UnifiedData** object and insert it into the UDMF public data channel. 55 56 ```ts 57 import { BusinessError } from '@ohos.base'; 58 let plainText = new unifiedDataChannel.PlainText(); 59 plainText.textContent = 'hello world!'; 60 let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 61 62 // Specify the type of the data channel to which the data is to be inserted. 63 let options: unifiedDataChannel.Options = { 64 intention: unifiedDataChannel.Intention.DATA_HUB 65 } 66 try { 67 unifiedDataChannel.insertData(options, unifiedData, (err, data) => { 68 if (err === undefined) { 69 console.info(`Succeeded in inserting data. key = ${data}`); 70 } else { 71 console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `); 72 } 73 }); 74 } catch (e) { 75 let error: BusinessError = e as BusinessError; 76 console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `); 77 } 78 ``` 793. Update the **UnifiedData** object inserted. 80 81 ```ts 82 import { BusinessError } from '@ohos.base'; 83 let plainText = new unifiedDataChannel.PlainText(); 84 plainText.textContent = 'How are you!'; 85 let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 86 87 // Specify the URI of the UnifiedData object to update. 88 let options: unifiedDataChannel.Options = { 89 key: 'udmf://DataHub/com.ohos.test/0123456789' 90 }; 91 92 try { 93 unifiedDataChannel.updateData(options, unifiedData, (err) => { 94 if (err === undefined) { 95 console.info('Succeeded in updating data.'); 96 } else { 97 console.error(`Failed to update data. code is ${err.code},message is ${err.message} `); 98 } 99 }); 100 } catch (e) { 101 let error: BusinessError = e as BusinessError; 102 console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `); 103 } 104 ``` 1054. Delete the **UnifiedData** object from the UDMF public data channel. 106 107 ```ts 108 import { BusinessError } from '@ohos.base'; 109 // Specify the type of the data channel whose data is to be deleted. 110 let options: unifiedDataChannel.Options = { 111 intention: unifiedDataChannel.Intention.DATA_HUB 112 }; 113 114 try { 115 unifiedDataChannel.deleteData(options, (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 **@ohos.data.unifiedDataChannel** and **@ohos.data.uniformTypeDescriptor** modules. 140 141 ```ts 142 import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 143 import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor'; 144 ``` 1452. Query the **UnifiedData** object in the UDMF public data channel. 146 147 ```ts 148 import { BusinessError } from '@ohos.base'; 149 // Specify the type of the data channel whose data is to be queried. 150 let options: unifiedDataChannel.Options = { 151 intention: unifiedDataChannel.Intention.DATA_HUB 152 }; 153 154 try { 155 unifiedDataChannel.queryData(options, (err, data) => { 156 if (err === undefined) { 157 console.info(`Succeeded in querying data. size = ${data.length}`); 158 for (let i = 0; i < data.length; i++) { 159 let records = data[i].getRecords(); 160 for (let j = 0; j < records.length; j++) { 161 if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 162 let text = records[j] as unifiedDataChannel.PlainText; 163 console.info(`${i + 1}.${text.textContent}`); 164 } 165 } 166 } 167 } else { 168 console.error(`Failed to query data. code is ${err.code},message is ${err.message} `); 169 } 170 }); 171 } catch(e) { 172 let error: BusinessError = e as BusinessError; 173 console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `); 174 } 175 ``` 176