1e41f4b71Sopenharmony_ci# Silent Access via the DatamgrService 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## When to Use 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciIn a typical cross-application data access scenario, the data provider may be started multiple times. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciTo minimize the startup times of the data provider and speed up data access, OpenHarmony provides the silent access feature, which allows access to the database without starting the data provider. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ciIn silent access, the DatamgrService accesses and modifies data without starting the data provider. 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciThe DatamgrService supports basic database access and data hosting only. If service processing is required, the service processing logic must be encapsulated into APIs for the data consumer to call. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ciIf the service is too complex, use [DataShareExtensionAbility](../reference/apis-arkdata/js-apis-application-dataShareExtensionAbility-sys.md) to start the data provider. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Working Principles 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciThe DatamgrService can serve as a proxy to access the following data: 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci- Persistent data: data in the database of the data provider. It is stored in the sandbox directory of the data provider and can be shared in declaration mode by the data provider. Persistent data is configured as data tables for access. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci- Process data: process data, in the JSON or byte format, managed by the DatamgrService. It is stored in the DatamgrService sandbox directory, and is automatically deleted 10 days after no subscription. 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci- Dynamic data: data stored in the memory of a device. It is automatically deleted after the device is restarted. Currently, the dynamic data refers to only the data set by **enableSilentProxy** and **disableSilentProxy**. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci| Data Type | Location | Data Format | Validity Period | Usage | 31e41f4b71Sopenharmony_ci| ----- | --------- | ----------- | ------------ | --------------------------------- | 32e41f4b71Sopenharmony_ci| Persistent data| Sandbox directory of the data provider | Database tables | Permanent | RDB data used for schedules and meetings. | 33e41f4b71Sopenharmony_ci| Process data | DatamgrService sandbox directory| JSON or byte| Automatically deleted 10 days after no subscription| Time-sensitive data in simple format used for step count, weather, and heart rate monitoring.| 34e41f4b71Sopenharmony_ci| Dynamic data | DatamgrService memory| KV pair| Automatically deleted after the device is restarted| Data generated when silent access is dynamically disabled or enabled. For example, to ensure data accuracy, silent access needs to be disabled in upgrade and enabled after the upgrade by using APIs. The "enabled" or "disabled" status generated by using the API is cleared after the device is restarted. The dynamic data refers to only the data set by **enableSilentProxy** and **disableSilentProxy**.| 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**Figure 1** Silent access 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci- In silent access, **DatamgrService** obtains the access rules configured by the data provider through directory mapping, performs preprocessing based on rules, and accesses the database. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci- To use silent access, the URIs must be in the following format: 45e41f4b71Sopenharmony_ci datashareproxy://{bundleName}/{dataPath} 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci The **DatamgrService** obtains the data provider application based on **bundleName**, reads the configuration, verifies the permission, and accesses data. 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci **dataPath** identifies the data. It can be customized and must be unique in the same data provider application. 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci## Constraints 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci- Currently, only the RDB stores support silent access. 55e41f4b71Sopenharmony_ci- The system supports a maximum of 16 concurrent query operations. Excess query requests need to be queued for processing. 56e41f4b71Sopenharmony_ci- The proxy is not allowed to create a database for persistent data. To create a database, you must start the data provider. 57e41f4b71Sopenharmony_ci- If the data provider is an application with a normal signature, the data read/write permission must be system_basic or higher. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci## Available APIs 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ciMost of the APIs for silent access are executed asynchronously in callback or promise mode. In the following table, callback-based APIs are used as an example. For more information about the APIs, see [Data Sharing](../reference/apis-arkdata/js-apis-data-dataShare-sys.md). 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci### Universal APIs 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci| API | Description | 67e41f4b71Sopenharmony_ci| ---------------------------------------- | -------------------- | 68e41f4b71Sopenharmony_ci| createDataShareHelper(context: Context, uri: string, options: DataShareHelperOptions, callback: AsyncCallback<DataShareHelper>): void | Creates a **DataShareHelper** instance.| 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci### APIs for Accessing Persistent Data 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci| API | Description | 73e41f4b71Sopenharmony_ci| ---------------------------------------- | -------------------- | 74e41f4b71Sopenharmony_ci| insert(uri: string, value: ValuesBucket, callback: AsyncCallback<number>): void | Inserts a row of data into a table. | 75e41f4b71Sopenharmony_ci| delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<number>): void | Deletes one or more data records from the database. | 76e41f4b71Sopenharmony_ci| query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: AsyncCallback<DataShareResultSet>): void | Queries data in the database. | 77e41f4b71Sopenharmony_ci| update(uri: string, predicates: dataSharePredicates.DataSharePredicates, value: ValuesBucket, callback: AsyncCallback<number>): void | Updates data in the database. | 78e41f4b71Sopenharmony_ci| addTemplate(uri: string, subscriberId: string, template: Template): void | Adds a data template with the specified subscriber. | 79e41f4b71Sopenharmony_ci| on(type: 'rdbDataChange', uris: Array<string>, templateId: TemplateId, callback: AsyncCallback<RdbDataChangeNode>): Array<OperationResult | Subscribes to the changes of the data corresponding to the specified URI and template.| 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci### APIs for Accessing Process Data 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci| API | Description | 84e41f4b71Sopenharmony_ci| ---------------------------------------- | ------------------ | 85e41f4b71Sopenharmony_ci| publish(data: Array<PublishedItem>, bundleName: string, version: number, callback: AsyncCallback<Array<OperationResult>>): void | Publish data to the DatamgrService.| 86e41f4b71Sopenharmony_ci| on(type: 'publishedDataChange', uris: Array<string>, subscriberId: string, callback: AsyncCallback<PublishedDataChangeNode>): Array<OperationResult> | Subscribes to changes of the published data. | 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci### APIs for Accessing Dynamic Data 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci| API | Description | 91e41f4b71Sopenharmony_ci| ---------------------------------------- | ------------------ | 92e41f4b71Sopenharmony_ci| enableSilentProxy(context: Context, uri?: string): Promise<void> | Enables silent access by the data provider dynamically.<br>When the data consumer calls the **DataShare** API through silent access, the system verifies the silent access status.<br>If silent access is enabled, the **DataShare** API will be executed.| 93e41f4b71Sopenharmony_ci| disableSilentProxy(context: Context, uri?: string): Promise<void> | Disables silent access by the data provider dynamically.<br>When the data consumer calls the **DataShare** API through silent access, the system verifies the silent access status.<br>If silent access is disabled, the **DataShare** API will be denied. | 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci## Accessing Persistent Data 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ciThe following walks you through on how to share an RDB store. 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci### Data Provider Application 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci1. In the **module.json5** file, set the data to be shared in **proxyData**. For details about the configuration, see [module.json5 Configuration File](../quick-start/module-configuration-file.md). 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci **Table 1** proxyData in module.json5 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci | Name | Description | Mandatory | 108e41f4b71Sopenharmony_ci | ----------------------- | ---------------------------------------- | ---- | 109e41f4b71Sopenharmony_ci | uri | URI of the data proxy, which is the unique identifier for cross-application data access. | Yes | 110e41f4b71Sopenharmony_ci | requiredReadPermission | Permission required for reading data from the data proxy. If this parameter is not set, other applications are not allowed to access data. For details about the permissions, see [Permissions for All Applications](../security/AccessToken/permissions-for-all.md). | No | 111e41f4b71Sopenharmony_ci | requiredWritePermission | Permission required for writing data to the data proxy. If this parameter is not set, other applications are not allowed to write data to the data proxy. For details about the permissions, see [Permissions for All Applications](../security/AccessToken/permissions-for-all.md). | No | 112e41f4b71Sopenharmony_ci | metadata | Metadata of the data source, including the **name** and **resource** fields.<br>The **name** field identifies the configuration, which has a fixed value of **dataProperties**.<br>The value of **resource** is **$profile:{fileName}**, indicating that the name of the configuration file is **{fileName}.json**.| Yes | 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci **module.json5 example** 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci ```json 117e41f4b71Sopenharmony_ci "proxyData":[ 118e41f4b71Sopenharmony_ci { 119e41f4b71Sopenharmony_ci "uri": "datashareproxy://com.acts.ohos.data.datasharetest/test", 120e41f4b71Sopenharmony_ci "requiredReadPermission": "ohos.permission.GET_BUNDLE_INFO", 121e41f4b71Sopenharmony_ci "requiredWritePermission": "ohos.permission.KEEP_BACKGROUND_RUNNING", 122e41f4b71Sopenharmony_ci "metadata": { 123e41f4b71Sopenharmony_ci "name": "dataProperties", 124e41f4b71Sopenharmony_ci "resource": "$profile:my_config" 125e41f4b71Sopenharmony_ci } 126e41f4b71Sopenharmony_ci } 127e41f4b71Sopenharmony_ci ] 128e41f4b71Sopenharmony_ci ``` 129e41f4b71Sopenharmony_ci **Table 2** Fields in my_config.json 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci | Name | Description | Mandatory | 132e41f4b71Sopenharmony_ci | ----- | ---------------------------------------- | ---- | 133e41f4b71Sopenharmony_ci | path | Data source path, in the **Database_name/Table_name** format. Currently, only RDB stores are supported. | Yes | 134e41f4b71Sopenharmony_ci | type | Database type. Currently, only **rdb** is supported. | Yes | 135e41f4b71Sopenharmony_ci | scope | Scope of the database.<br>- **module** indicates that the database is located in this module.<br>- **application** indicates that the database is located in this application.| No | 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci **my_config.json example** 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci ```json 140e41f4b71Sopenharmony_ci { 141e41f4b71Sopenharmony_ci "path": "DB00/TBL00", 142e41f4b71Sopenharmony_ci "type": "rdb", 143e41f4b71Sopenharmony_ci "scope": "application" 144e41f4b71Sopenharmony_ci } 145e41f4b71Sopenharmony_ci ``` 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci### Data Consumer Application 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci1. Import dependencies. 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci ```ts 153e41f4b71Sopenharmony_ci import { dataShare, dataSharePredicates, ValuesBucket } from '@kit.ArkData'; 154e41f4b71Sopenharmony_ci import { UIAbility } from '@kit.AbilityKit'; 155e41f4b71Sopenharmony_ci import { window } from '@kit.ArkUI'; 156e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit' 157e41f4b71Sopenharmony_ci ``` 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci2. Define the URI string for communicating with the data provider. 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci ```ts 162e41f4b71Sopenharmony_ci let dseUri = ('datashareproxy://com.acts.ohos.data.datasharetest/test'); 163e41f4b71Sopenharmony_ci ``` 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci3. Create a **DataShareHelper** instance. 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci ```ts 168e41f4b71Sopenharmony_ci let dsHelper: dataShare.DataShareHelper | undefined = undefined; 169e41f4b71Sopenharmony_ci let abilityContext: Context; 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci export default class EntryAbility extends UIAbility { 172e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 173e41f4b71Sopenharmony_ci abilityContext = this.context; 174e41f4b71Sopenharmony_ci dataShare.createDataShareHelper(abilityContext, dseUri, { 175e41f4b71Sopenharmony_ci isProxy: true 176e41f4b71Sopenharmony_ci }, (err, data) => { 177e41f4b71Sopenharmony_ci dsHelper = data; 178e41f4b71Sopenharmony_ci }); 179e41f4b71Sopenharmony_ci } 180e41f4b71Sopenharmony_ci } 181e41f4b71Sopenharmony_ci ``` 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci4. Use **DataShareHelper** APIs to access the services provided by the provider, for example, adding, deleting, modifying, and querying data. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci ```ts 186e41f4b71Sopenharmony_ci // Construct a piece of data. 187e41f4b71Sopenharmony_ci let key1 = 'name'; 188e41f4b71Sopenharmony_ci let key2 = 'age'; 189e41f4b71Sopenharmony_ci let key3 = 'isStudent'; 190e41f4b71Sopenharmony_ci let key4 = 'Binary'; 191e41f4b71Sopenharmony_ci let valueName1 = 'ZhangSan'; 192e41f4b71Sopenharmony_ci let valueName2 = 'LiSi'; 193e41f4b71Sopenharmony_ci let valueAge1 = 21; 194e41f4b71Sopenharmony_ci let valueAge2 = 18; 195e41f4b71Sopenharmony_ci let valueIsStudent1 = false; 196e41f4b71Sopenharmony_ci let valueIsStudent2 = true; 197e41f4b71Sopenharmony_ci let valueBinary = new Uint8Array([1, 2, 3]); 198e41f4b71Sopenharmony_ci let valuesBucket: ValuesBucket = { key1: valueName1, key2: valueAge1, key3: valueIsStudent1, key4: valueBinary }; 199e41f4b71Sopenharmony_ci let updateBucket: ValuesBucket = { key1: valueName2, key2: valueAge2, key3: valueIsStudent2, key4: valueBinary }; 200e41f4b71Sopenharmony_ci let predicates = new dataSharePredicates.DataSharePredicates(); 201e41f4b71Sopenharmony_ci let valArray = ['*']; 202e41f4b71Sopenharmony_ci if (dsHelper != undefined) { 203e41f4b71Sopenharmony_ci // Insert a piece of data. 204e41f4b71Sopenharmony_ci (dsHelper as dataShare.DataShareHelper).insert(dseUri, valuesBucket, (err, data) => { 205e41f4b71Sopenharmony_ci console.info(`dsHelper insert result:${data}`); 206e41f4b71Sopenharmony_ci }); 207e41f4b71Sopenharmony_ci // Update data. 208e41f4b71Sopenharmony_ci (dsHelper as dataShare.DataShareHelper).update(dseUri, predicates, updateBucket, (err, data) => { 209e41f4b71Sopenharmony_ci console.info(`dsHelper update result:${data}`); 210e41f4b71Sopenharmony_ci }); 211e41f4b71Sopenharmony_ci // Query data. 212e41f4b71Sopenharmony_ci (dsHelper as dataShare.DataShareHelper).query(dseUri, predicates, valArray, (err, data) => { 213e41f4b71Sopenharmony_ci console.info(`dsHelper query result:${data}`); 214e41f4b71Sopenharmony_ci }); 215e41f4b71Sopenharmony_ci // Delete data. 216e41f4b71Sopenharmony_ci (dsHelper as dataShare.DataShareHelper).delete(dseUri, predicates, (err, data) => { 217e41f4b71Sopenharmony_ci console.info(`dsHelper delete result:${data}`); 218e41f4b71Sopenharmony_ci }); 219e41f4b71Sopenharmony_ci } 220e41f4b71Sopenharmony_ci ``` 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci5. Subscribe to data. 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci ```ts 225e41f4b71Sopenharmony_ci function onCallback(err: BusinessError, node: dataShare.RdbDataChangeNode) { 226e41f4b71Sopenharmony_ci console.info("uri " + JSON.stringify(node.uri)); 227e41f4b71Sopenharmony_ci console.info("templateId " + JSON.stringify(node.templateId)); 228e41f4b71Sopenharmony_ci console.info("data length " + node.data.length); 229e41f4b71Sopenharmony_ci for (let i = 0; i < node.data.length; i++) { 230e41f4b71Sopenharmony_ci console.info("data " + node.data[i]); 231e41f4b71Sopenharmony_ci } 232e41f4b71Sopenharmony_ci } 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci let key21: string = "p1"; 235e41f4b71Sopenharmony_ci let value21: string = "select * from TBL00"; 236e41f4b71Sopenharmony_ci let key22: string = "p2"; 237e41f4b71Sopenharmony_ci let value22: string = "select name from TBL00"; 238e41f4b71Sopenharmony_ci let template: dataShare.Template = { 239e41f4b71Sopenharmony_ci predicates: { 240e41f4b71Sopenharmony_ci key21: value21, 241e41f4b71Sopenharmony_ci key22: value22, 242e41f4b71Sopenharmony_ci }, 243e41f4b71Sopenharmony_ci scheduler: "" 244e41f4b71Sopenharmony_ci } 245e41f4b71Sopenharmony_ci if(dsHelper != undefined) 246e41f4b71Sopenharmony_ci { 247e41f4b71Sopenharmony_ci (dsHelper as dataShare.DataShareHelper).addTemplate(dseUri, "111", template); 248e41f4b71Sopenharmony_ci } 249e41f4b71Sopenharmony_ci let templateId: dataShare.TemplateId = { 250e41f4b71Sopenharmony_ci subscriberId: "111", 251e41f4b71Sopenharmony_ci bundleNameOfOwner: "com.acts.ohos.data.datasharetestclient" 252e41f4b71Sopenharmony_ci } 253e41f4b71Sopenharmony_ci if(dsHelper != undefined) { 254e41f4b71Sopenharmony_ci // When the DatamgrService modifies data, onCallback is invoked to return the data queried based on the rules in the template. 255e41f4b71Sopenharmony_ci let result: Array<dataShare.OperationResult> = (dsHelper as dataShare.DataShareHelper).on("rdbDataChange", [dseUri], templateId, onCallback); 256e41f4b71Sopenharmony_ci } 257e41f4b71Sopenharmony_ci ``` 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci## Accessing Process Data 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ciThe following walks you through on how to host process data. 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci### (Optional) Data Provider Application 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ciIn the **module.json5** file, set the data to be hosted in **proxyData**. For details about the configuration, see [module.json5 Configuration File](../quick-start/module-configuration-file.md). 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci> **NOTE** 268e41f4b71Sopenharmony_ci> 269e41f4b71Sopenharmony_ci> - The configuration of **proxyData** is optional. 270e41f4b71Sopenharmony_ci> - If **proxyData** is not configured, the hosted data cannot be accessed by other applications. 271e41f4b71Sopenharmony_ci> - If **proxyData** is not configured, you do not need to use the full data path. For example, you can use **weather** instead of **datashareproxy://com.acts.ohos.data.datasharetest/weather** when publishing, subscribing to, and querying data. 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci**Table 3** proxyData in module.json5 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci| Name | Description | Mandatory | 276e41f4b71Sopenharmony_ci| ----------------------- | ----------------------------- | ---- | 277e41f4b71Sopenharmony_ci| uri | URI of the data proxy, which is the unique identifier for cross-application data access. | Yes | 278e41f4b71Sopenharmony_ci| requiredReadPermission | Permission required for reading data from the data proxy. If this parameter is not set, other applications are not allowed to access data. For details about the permissions, see [Permissions for All Applications](../security/AccessToken/permissions-for-all.md).| No | 279e41f4b71Sopenharmony_ci| requiredWritePermission | Permission required for writing data to the data proxy. If this parameter is not set, other applications are not allowed to write data to the dta proxy. For details about the permissions, see [Permissions for All Applications](../security/AccessToken/permissions-for-all.md).| No | 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci**module.json5 example** 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci```json 284e41f4b71Sopenharmony_ci"proxyData": [ 285e41f4b71Sopenharmony_ci { 286e41f4b71Sopenharmony_ci "uri": "datashareproxy://com.acts.ohos.data.datasharetest/weather", 287e41f4b71Sopenharmony_ci "requiredReadPermission": "ohos.permission.GET_BUNDLE_INFO", 288e41f4b71Sopenharmony_ci "requiredWritePermission": "ohos.permission.KEEP_BACKGROUND_RUNNING" 289e41f4b71Sopenharmony_ci } 290e41f4b71Sopenharmony_ci] 291e41f4b71Sopenharmony_ci``` 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci### Data Consumer Application 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci1. Import dependencies. 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci ```ts 298e41f4b71Sopenharmony_ci import { dataShare } from '@kit.ArkData'; 299e41f4b71Sopenharmony_ci import { UIAbility } from '@kit.AbilityKit'; 300e41f4b71Sopenharmony_ci import { window } from '@kit.ArkUI'; 301e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 302e41f4b71Sopenharmony_ci ``` 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci2. Define the URI string for communicating with the data provider. 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci ```ts 307e41f4b71Sopenharmony_ci let dseUri = ('datashareproxy://com.acts.ohos.data.datasharetest/weather'); 308e41f4b71Sopenharmony_ci ``` 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci3. Create a **DataShareHelper** instance. 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci ```ts 313e41f4b71Sopenharmony_ci let dsHelper: dataShare.DataShareHelper | undefined = undefined; 314e41f4b71Sopenharmony_ci let abilityContext: Context; 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci export default class EntryAbility extends UIAbility { 317e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 318e41f4b71Sopenharmony_ci abilityContext = this.context; 319e41f4b71Sopenharmony_ci dataShare.createDataShareHelper(abilityContext, dseUri, {isProxy : true}, (err, data) => { 320e41f4b71Sopenharmony_ci dsHelper = data; 321e41f4b71Sopenharmony_ci }); 322e41f4b71Sopenharmony_ci } 323e41f4b71Sopenharmony_ci } 324e41f4b71Sopenharmony_ci ``` 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci4. Use the **DataShareHelper** APIs to access the services provided by the provider, for example, adding, deleting, modifying, and querying data. 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci ```ts 329e41f4b71Sopenharmony_ci // Construct two pieces of data. The first data is not configured with proxyDatas and cannot be accessed by other applications. 330e41f4b71Sopenharmony_ci let data : Array<dataShare.PublishedItem> = [ 331e41f4b71Sopenharmony_ci {key:"city", subscriberId:"11", data:"xian"}, 332e41f4b71Sopenharmony_ci {key:"datashareproxy://com.acts.ohos.data.datasharetest/weather", subscriberId:"11", data:JSON.stringify("Qing")}]; 333e41f4b71Sopenharmony_ci // Publish data. 334e41f4b71Sopenharmony_ci if (dsHelper != undefined) { 335e41f4b71Sopenharmony_ci let result: Array<dataShare.OperationResult> = await (dsHelper as dataShare.DataShareHelper).publish(data, "com.acts.ohos.data.datasharetestclient"); 336e41f4b71Sopenharmony_ci } 337e41f4b71Sopenharmony_ci ``` 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci5. Subscribe to data. 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci ```ts 342e41f4b71Sopenharmony_ci function onPublishCallback(err: BusinessError, node:dataShare.PublishedDataChangeNode) { 343e41f4b71Sopenharmony_ci console.info("onPublishCallback"); 344e41f4b71Sopenharmony_ci } 345e41f4b71Sopenharmony_ci let uris:Array<string> = ["city", "datashareproxy://com.acts.ohos.data.datasharetest/weather"]; 346e41f4b71Sopenharmony_ci if (dsHelper != undefined) { 347e41f4b71Sopenharmony_ci let result: Array<dataShare.OperationResult> = (dsHelper as dataShare.DataShareHelper).on("publishedDataChange", uris, "11", onPublishCallback); 348e41f4b71Sopenharmony_ci } 349e41f4b71Sopenharmony_ci ``` 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci## Accessing Dynamic Data 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ciOnly the data provider is involved. The following walks you through on how to dynamically enable silent access. 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci### Data Provider Application 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ciThe data provider calls the **enableSilentProxy** API to dynamically enable silent access. This API must be used with the **isSilentProxyEnable** field in the **data_share_config.json** file. For details, see [**data_share_config.json** configuration](./share-data-by-datashareextensionability.md). 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci> **NOTE** 360e41f4b71Sopenharmony_ci> 361e41f4b71Sopenharmony_ci> - In the **data_share_config.json** file, the default value of **isSilentProxyEnable** is **true**, which means silent access is enabled. 362e41f4b71Sopenharmony_ci> - To verify whether silent access is enabled, the system first checks the silent access status set by the **enableSilentProxy** or **disableSilentProxy** API called, and then checks the value of **isSilentProxyEnable** in the **data_share_config.json** file. 363e41f4b71Sopenharmony_ci> - If **enableSilentProxy** or **disableSilentProxy** has not been called, the value of **isSilentProxyEnable** in the **data_share_config.json** file is preferentially checked. 364e41f4b71Sopenharmony_ci> - Silent access is enabled by default if **enableSilentProxy** or **disableSilentProxy** has not been called and **isSilentProxyEnable** in the **data_share_config.json** file is not configured. 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci1. Import dependencies. 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci ```ts 369e41f4b71Sopenharmony_ci import { dataShare } from '@kit.ArkData'; 370e41f4b71Sopenharmony_ci import { UIAbility } from '@kit.AbilityKit'; 371e41f4b71Sopenharmony_ci import { window } from '@kit.ArkUI'; 372e41f4b71Sopenharmony_ci ``` 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci2. Define the URI string for communicating with the data provider. 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci ```ts 377e41f4b71Sopenharmony_ci let dseUri = ('datashare:///com.acts.datasharetest/entry/DB00/TBL00'); 378e41f4b71Sopenharmony_ci ``` 379e41f4b71Sopenharmony_ci 380e41f4b71Sopenharmony_ci3. Create a **DataShareHelper** instance. 381e41f4b71Sopenharmony_ci 382e41f4b71Sopenharmony_ci ```ts 383e41f4b71Sopenharmony_ci let abilityContext: Context; 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci export default class EntryAbility extends UIAbility { 386e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 387e41f4b71Sopenharmony_ci abilityContext = this.context; 388e41f4b71Sopenharmony_ci dataShare.enableSilentProxy(abilityContext, dseUri); 389e41f4b71Sopenharmony_ci } 390e41f4b71Sopenharmony_ci } 391e41f4b71Sopenharmony_ci ``` 392