1# Sharing Data Using DataShareExtensionAbility 2 3 4## When to Use 5 6If complex services are involved in cross-application data access, you can use **DataShareExtensionAbility** to start the application of the data provider to implement data access. 7 8You need to implement flexible service logics via callbacks of the service provider. 9 10 11## Working Principles 12 13There are two roles in **DataShare**: 14 15- Data provider: implements operations, such as adding, deleting, modifying, and querying data, and opening a file, using [DataShareExtensionAbility](../reference/apis-arkdata/js-apis-application-dataShareExtensionAbility-sys.md). 16 17- Data consumer: accesses the data provided by the provider using [createDataShareHelper()](../reference/apis-arkdata/js-apis-data-dataShare-sys.md#datasharecreatedatasharehelper). 18 19**Figure 1** Data sharing mechanism 20 21 22- The **DataShareExtensionAbility** module, as the data provider, implements services related to data sharing between applications. 23 24- The **DataShareHelper** module, as the data consumer, provides APIs for accessing data, including adding, deleting, modifying, and querying data. 25 26- The data consumer communicates with the data provider via inter-process communication (IPC). The data provider can be implemented through a database or other data storage. 27 28- The **ResultSet** module is implemented through shared memory. Shared memory stores the result sets, and interfaces are provided to traverse result sets. 29 30## How to Develop 31 32 33### Data Provider Application Development (for System Applications Only) 34 35The [DataShareExtensionAbility](../reference/apis-arkdata/js-apis-application-dataShareExtensionAbility-sys.md) provides the following APIs. You can override these APIs as required. 36 37- **onCreate**: called by the server to initialize service logic when the DataShare client connects to the DataShareExtensionAbility server. 38- **insert**: called to insert data upon the request of the client. Data insertion must be implemented in this callback on the server. 39- **update**: called to update data upon the request of the client. Data update must be implemented in this callback on the server. 40- **batchUpdate**: called to update batch data upon the request of the client. Batch data update must be implemented in this callback on the server. 41- **delete**: called to delete data upon the request of the client. Data deletion must be implemented in this callback on the server. 42- **query**: called to query data upon the request of the client. Data query must be implemented in this callback on the server. 43- **batchInsert**: called to batch insert data upon the request of the client. Batch data insertion must be implemented in this callback on the server. 44- **normalizeUri**: converts the URI provided by the client to the URI used by the server. 45- **denormalizeUri**: converts the URI used by the server to the initial URI passed by the client. 46 47Before implementing a **DataShare** service, you need to create a **DataShareExtensionAbility** object in the DevEco Studio project as follows: 48 491. In the **ets** directory of the **Module** project, right-click and choose **New > Directory** to create a directory named **DataShareExtAbility**. 50 512. Right-click the **DataShareAbility** directory, and choose **New > ArkTS File** to create a file named **DataShareExtAbility.ets**. 52 533. In the **DataShareExtAbility.ets** file, import the **DataShareExtensionAbility** module. You can override the service implementation as required. For example, if the data provider provides only the insert, delete, and query services, you can override only these APIs and import the dependency modules. If permission verification is required, override the callbacks using [getCallingPid](../reference/apis-ipc-kit/js-apis-rpc.md#getcallingpid), [getCallingUid](../reference/apis-ipc-kit/js-apis-rpc.md#getcallinguid), and [getCallingTokenId](../reference/apis-ipc-kit/js-apis-rpc.md#getcallingtokenid8) provided by the IPC module to obtain the data consumer information for permission verification. 54 55 ```ts 56 import { DataShareExtensionAbility, dataShare, dataSharePredicates, relationalStore, DataShareResultSet } from '@kit.ArkData'; 57 import { Want } from '@kit.AbilityKit'; 58 import { BusinessError } from '@kit.BasicServicesKit' 59 ``` 60 614. Implement the data provider services. For example, implement data storage of the data provider by creating and using a database, reading and writing files, or accessing the network. 62 63 ```ts 64 const DB_NAME = 'DB00.db'; 65 const TBL_NAME = 'TBL00'; 66 const DDL_TBL_CREATE = "CREATE TABLE IF NOT EXISTS " 67 + TBL_NAME 68 + ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, isStudent BOOLEAN, Binary BINARY)'; 69 70 let rdbStore: relationalStore.RdbStore; 71 let result: string; 72 73 export default class DataShareExtAbility extends DataShareExtensionAbility { 74 // Override onCreate(). 75 onCreate(want: Want, callback: Function) { 76 result = this.context.cacheDir + '/datashare.txt'; 77 // Create an RDB store. 78 relationalStore.getRdbStore(this.context, { 79 name: DB_NAME, 80 securityLevel: relationalStore.SecurityLevel.S3 81 }, (err:BusinessError, data:relationalStore.RdbStore) => { 82 rdbStore = data; 83 rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => { 84 console.info(`DataShareExtAbility onCreate, executeSql done err:${err}`); 85 }); 86 if (callback) { 87 callback(); 88 } 89 }); 90 } 91 92 // Override query(). 93 query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) { 94 if (predicates === null || predicates === undefined) { 95 console.info('invalid predicates'); 96 } 97 try { 98 rdbStore.query(TBL_NAME, predicates, columns, (err:BusinessError, resultSet:relationalStore.ResultSet) => { 99 if (resultSet !== undefined) { 100 console.info(`resultSet.rowCount:${resultSet.rowCount}`); 101 } 102 if (callback !== undefined) { 103 callback(err, resultSet); 104 } 105 }); 106 } catch (err) { 107 let code = (err as BusinessError).code; 108 let message = (err as BusinessError).message 109 console.error(`Failed to query. Code:${code},message:${message}`); 110 } 111 } 112 // Override the batchUpdate API. 113 batchUpdate(operations:Record<string, Array<dataShare.UpdateOperation>>, callback:Function) { 114 let recordOps : Record<string, Array<dataShare.UpdateOperation>> = operations; 115 let results : Record<string, Array<number>> = {}; 116 let a = Object.entries(recordOps); 117 for (let i = 0; i < a.length; i++) { 118 let key = a[i][0]; 119 let values = a[i][1]; 120 let result : number[] = []; 121 for (const value of values) { 122 rdbStore.update(TBL_NAME, value.values, value.predicates).then(async (rows) => { 123 console.info('Update row count is ' + rows); 124 result.push(rows); 125 }).catch((err:BusinessError) => { 126 console.info('Update failed, err is ' + JSON.stringify(err)); 127 result.push(-1) 128 }) 129 } 130 results[key] = result; 131 } 132 callback(null, results); 133 } 134 // Override other APIs as required. 135 }; 136 ``` 137 1385. Define **DataShareExtensionAbility** in **module.json5**. 139 140 **Table 1** Fields in module.json5 141 142 | Field| Description| Mandatory| 143 | -------- | -------- | -------- | 144 | name | Ability name, corresponding to the **ExtensionAbility** class name derived from **Ability**.| Yes| 145 | type | Ability type. The value **dataShare** indicates that the development is based on the **datashare** template.| Yes| 146 | uri | Unique identifier for the data consumer to access the data provider.<br> You can add suffix parameters to set the target access object. The suffix parameters must be added to the end of the URI and start with a question mark (?).<br> - Currently, only the **user** parameter is supported.<br> - The value of **user** must be an integer. It indicates the user ID of the data provider. If It is not specified, the user ID of the data consumer is used. For details about the definition of **user** and how to obtain it, see [user](../reference/apis-basic-services-kit/js-apis-osAccount.md#getactivatedosaccountlocalids9).<br> - Currently, the data consumer in cross-user access must have the ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS permission.| Yes| 147 | exported | Whether it is visible to other applications. Data sharing is allowed only when the value is **true**.| Yes| 148 | readPermission | Permission required for accessing data. If this parameter is not set, read permission verification is not performed by default.<br>**NOTE**: The permission constraints for **DataShareExtensionAbility** are different from that for silent access. It is important to understand the difference and prevent confusion. For details, see [Silent Access via DatamgrService](share-data-by-silent-access.md).| No| 149 | writePermission | Permission required for modifying data. If this parameter is not set, write permission verification is not performed by default.<br>**NOTE**: The permission constraints for **DataShareExtensionAbility** are different from that for silent access. It is important to understand the difference and prevent confusion. For details, see [Silent Access via DatamgrService](share-data-by-silent-access.md).| No| 150 | metadata | Silent access configuration, which includes the following:<br>- **name**: identifies the configuration, which has a fixed value of **ohos.extension.dataShare**.<br>- **resource**: has a fixed value of **$profile:data_share_config**, which indicates that the profile name is **data_share_config.json**.| **metadata** is mandatory when the ability launch type is **singleton**. For details about the ability launch type, see **launchType** in the [Internal Structure of the abilities Attribute](../quick-start/module-structure.md#internal-structure-of-the-abilities-attribute).| 151 152 **module.json5 example** 153 154 ```json 155 // The following uses settingsdata as an example. 156 "extensionAbilities": [ 157 { 158 "srcEntry": "./ets/DataAbility/DataExtAbility.ets", 159 "name": "DataExtAbility", 160 "icon": "$media:icon", 161 "description": "$string:description_datashareextability", 162 "type": "dataShare", 163 "uri": "datashare://com.ohos.settingsdata.DataAbility", 164 "exported": true, 165 // Configure permissions based on actual situation. The permissions configured here are examples only. 166 "readPermission": "ohos.permission.MANAGE_SECURE_SETTINGS", 167 "writePermission": "ohos.permission.MANAGE_SECURE_SETTINGS", 168 "metadata": [{"name": "ohos.extension.dataShare", "resource": "$profile:data_share_config"}] 169 } 170 ] 171 ``` 172 173 **Table 2** Fields in the data_share_config.json file 174 175 | Field | Description | Mandatory| 176 | ------------------- | ------------------------------------------------------------ | ---- | 177 | tableConfig | Configuration label, which includes **uri** and **crossUserMode**.<br>- **uri**: specifies the range for which the configuration takes effect. The URI supports the following formats in descending order by priority:<br> 1. *****: indicates all databases and tables.<br> 2. **datashare:///{*bundleName*}/{*moduleName*}/{*storeName*}**: specifies a database.<br> 3. **datashare:///{*bundleName*}/{*moduleName*}/{*storeName*}/{*tableName*}**: specifies a table.<br>If URIs of different formats are configured, only the URI with the higher priority takes effect.<br>- **crossUserMode**: Whether to share data between multiple users.<br>The value **1** means to share data between multiple users, and the value **2** means the opposite.| Yes | 178 | isSilentProxyEnable | Whether to enable silent access for this ExtensionAbility.<br>The value **true** means to enable silent access, and the value **false** means the opposite.<br>The default value is **true**.<br>If an application has multiple ExtensionAbilities and this field is set to **false** for one of them, silent access is disabled for the application.<br>If the data provider has called **enableSilentProxy** or **disableSilentProxy**, silent access is enabled or disabled based on the API settings. Otherwise, the setting here takes effect.| No | 179 | launchInfos | Launch information, which includes **storeId** and **tableNames**.<br>If the data in a table involved in the configuration changes, an extensionAbility will be started based on the URI in **extensionAbilities**. You need to set this parameter only when the service needs to start an extensionAbility to process data that is not actively changed by the service.<br>- **storeId**: database name, excluding the file name extension. For example, if the database name is **test.db**, set this parameter to **test**.<br>- **tableNames**: names of the database tables. Any change in a table will start an extensionAbility.| No | 180 181 **data_share_config.json Example** 182 183 ```json 184 { 185 "tableConfig":[ 186 { 187 "uri":"*", 188 "crossUserMode":1 189 }, 190 { 191 "uri":"datashare:///com.ohos.settingsdata/entry/DB00", 192 "crossUserMode":1 193 }, 194 { 195 "uri":"datashare:///com.acts.datasharetest/entry/DB00/TBL00", 196 "crossUserMode":2 197 } 198 ], 199 "isSilentProxyEnable":true, 200 "launchInfos":[ 201 { 202 "storeId": "test", 203 "tableNames":["test1", "test2"] 204 } 205 ] 206 } 207 ``` 208 209 210### Data Consumer Application Development 211 2121. Import the dependencies. 213 214 ```ts 215 import { UIAbility } from '@kit.AbilityKit'; 216 import { dataShare, dataSharePredicates, DataShareResultSet, ValuesBucket } from '@kit.ArkData'; 217 import { window } from '@kit.ArkUI'; 218 import { BusinessError } from '@kit.BasicServicesKit'; 219 ``` 220 2212. Define the URI string for communicating with the data provider. 222 223 ```ts 224 // Different from the URI defined in the module.json5 file, the URI passed in the parameter has an extra slash (/), because there is a DeviceID parameter between the second and the third slash (/). 225 let dseUri = ('datashare:///com.ohos.settingsdata.DataAbility'); 226 ``` 227 2283. Create a **DataShareHelper** instance. 229 230 ```ts 231 let dsHelper: dataShare.DataShareHelper | undefined = undefined; 232 let abilityContext: Context; 233 234 export default class EntryAbility extends UIAbility { 235 onWindowStageCreate(windowStage: window.WindowStage) { 236 abilityContext = this.context; 237 dataShare.createDataShareHelper(abilityContext, dseUri, (err, data) => { 238 dsHelper = data; 239 }); 240 } 241 } 242 ``` 243 2444. Use the APIs provided by **DataShareHelper** to access the services provided by the provider, for example, adding, deleting, modifying, and querying data. 245 246 ```ts 247 // Construct a piece of data. 248 let key1 = 'name'; 249 let key2 = 'age'; 250 let key3 = 'isStudent'; 251 let key4 = 'Binary'; 252 let valueName1 = 'ZhangSan'; 253 let valueName2 = 'LiSi'; 254 let valueAge1 = 21; 255 let valueAge2 = 18; 256 let valueIsStudent1 = false; 257 let valueIsStudent2 = true; 258 let valueBinary = new Uint8Array([1, 2, 3]); 259 let valuesBucket: ValuesBucket = { key1: valueName1, key2: valueAge1, key3: valueIsStudent1, key4: valueBinary }; 260 let updateBucket: ValuesBucket = { key1: valueName2, key2: valueAge2, key3: valueIsStudent2, key4: valueBinary }; 261 let predicates = new dataSharePredicates.DataSharePredicates(); 262 let valArray = ['*']; 263 264 let record: Record<string, Array<dataShare.UpdateOperation>> = {}; 265 let operations1: Array<dataShare.UpdateOperation> = []; 266 let operations2: Array<dataShare.UpdateOperation> = []; 267 let operation1: dataShare.UpdateOperation = { 268 values: valuesBucket, 269 predicates: predicates 270 } 271 operations1.push(operation1); 272 let operation2: dataShare.UpdateOperation = { 273 values: updateBucket, 274 predicates: predicates 275 } 276 operations2.push(operation2); 277 record["uri1"] = operations1; 278 record["uri2"] = operations2; 279 280 if (dsHelper != undefined) { 281 // Insert a piece of data. 282 (dsHelper as dataShare.DataShareHelper).insert(dseUri, valuesBucket, (err:BusinessError, data:number) => { 283 console.info(`dsHelper insert result:${data}`); 284 }); 285 // Update data. 286 (dsHelper as dataShare.DataShareHelper).update(dseUri, predicates, updateBucket, (err:BusinessError, data:number) => { 287 console.info(`dsHelper update result:${data}`); 288 }); 289 // Query data. 290 (dsHelper as dataShare.DataShareHelper).query(dseUri, predicates, valArray, (err:BusinessError, data:DataShareResultSet) => { 291 console.info(`dsHelper query result:${data}`); 292 }); 293 // Delete data. 294 (dsHelper as dataShare.DataShareHelper).delete(dseUri, predicates, (err:BusinessError, data:number) => { 295 console.info(`dsHelper delete result:${data}`); 296 }); 297 // Update data in batches. 298 (dsHelper as dataShare.DataShareHelper).batchUpdate(record).then((data: Record<string, Array<number>>) => { 299 // Traverse data to obtain the update result of each data record. value indicates the number of data records that are successfully updated. If value is less than 0, the update fails. 300 let a = Object.entries(data); 301 for (let i = 0; i < a.length; i++) { 302 let key = a[i][0]; 303 let values = a[i][1] 304 console.info(`Update uri:${key}`); 305 for (const value of values) { 306 console.info(`Update result:${value}`); 307 } 308 } 309 }); 310 // Close the DataShareHelper instance. 311 (dsHelper as dataShare.DataShareHelper).close(); 312 } 313 ``` 314