harmony 鸿蒙Distributed Data Service Development

  • 2022-08-09
  • 浏览 (669)

Distributed Data Service Development

When to Use

The Distributed Data Service (DDS) implements synchronization of application data across user devices. When data is added, deleted, or modified for an application on a device, the same application on another device can obtain the updated data. The DDS applies to the distributed gallery, messages, contacts, and file manager.

Available APIs

For details about the APIs, see Distributed KV Store.

Table 1 APIs provided by the DDS

API Description
createKVManager(config: KVManagerConfig, callback: AsyncCallback<KVManager>): void
createKVManager(config: KVManagerConfig): Promise<KVManager>
Creates a KvManager object for database management.
getKVStore<T extends KVStore>(storeId: string, options: Options, callback: AsyncCallback<T>): void
getKVStore<T extends KVStore>(storeId: string, options: Options): Promise<T>
Creates and obtains a KV store.
put(key: string, value: Uint8Array|string|number|boolean, callback: AsyncCallback<void>): void
put(key: string, value: Uint8Array|string|number|boolean): Promise<void>
Inserts and updates data.
delete(key: string, callback: AsyncCallback<void>): void
delete(key: string): Promise<void>
Deletes data.
get(key: string, callback: AsyncCallback<Uint8Array|string|boolean|number>): void
get(key: string): Promise<Uint8Array|string|boolean|number>
Obtains data.
on(event: ‘dataChange’, type: SubscribeType, observer: Callback<ChangeNotification>): void
on(event: ‘syncComplete’, syncCallback: Callback<Array<[string,number]>>): void
Subscribes to data changes in the KV store.
sync(deviceIdList: string[], mode: SyncMode, delayMs?: number): void Triggers database synchronization in manual mode.

How to Develop

The following uses a single KV store as an example to describe the development procedure.

  1. Import the distributed data module.
   import distributedKVStore from '@ohos.data.distributedKVStore';
  1. Apply for the required permission if data synchronization is required.

Add the permission required (FA model) in the config.json file. The sample code is as follows:

```json
{
  "module": {
      "reqPermissions": [
          {
             "name": "ohos.permission.DISTRIBUTED_DATASYNC"
          }
      ]
  }
}
```

For the apps based on the stage model, see Declaring Permissions.

This permission must also be granted by the user when the application is started for the first time. The sample code is as follows:

```js
// FA model
import featureAbility from '@ohos.ability.featureAbility';

function grantPermission() {
console.info('grantPermission');
let context = featureAbility.getContext();
context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666).then((data) => {
    console.info('success: ${data}');
}).catch((error) => {
    console.error('failed: ${error}');
})
}

grantPermission();

// Stage model
import UIAbility from '@ohos.app.ability.UIAbility';

let context = null;

class EntryAbility  extends UIAbility  {
  onWindowStageCreate(windowStage) {
    let context = this.context;
  }
}

function grantPermission() {
  let permissions = ['ohos.permission.DISTRIBUTED_DATASYNC'];
  context.requestPermissionsFromUser(permissions).then((data) => {
    console.log('success: ${data}');
  }).catch((error) => {
    console.error('failed: ${error}');
  });
}

grantPermission();
```
  1. Create a KvManager instance based on the specified KvManagerConfig object.

    1. Create a kvManagerConfig object based on the application context.
    2. Create a KvManager instance.

The sample code is as follows:

   // Obtain the context of the FA model.
   import featureAbility from '@ohos.ability.featureAbility';
   let context = featureAbility.getContext();
   
   // Obtain the context of the stage model.
   import UIAbility from '@ohos.app.ability.UIAbility';
   let context = null;
   class EntryAbility extends UIAbility {
      onWindowStageCreate(windowStage){
        context = this.context;
      }
   }
   
   let kvManager;
   try {
     const kvManagerConfig = {
       bundleName: 'com.example.datamanagertest',
       context:context,
     }
     distributedKVStore.createKVManager(kvManagerConfig, function (err, manager) {
       if (err) {
         console.error(`Failed to create KVManager. code is ${err.code},message is ${err.message}`);
         return;
       }
       console.log('Created KVManager successfully');
       kvManager = manager;
     });
   } catch (e) {
     console.error(`An unexpected error occurred.code is ${e.code},message is ${e.message}`);
   }
  1. Create and obtain a single KV store.

    1. Declare the ID of the single KV store to create.
    2. Create a single KV store. You are advised to disable automatic synchronization (autoSync:false) and call sync when a synchronization is required.

The sample code is as follows:

   let kvStore;
   try {
     const options = {
       createIfMissing: true,
       encrypt: false,
       backup: false,
       autoSync: false,
       kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION,
       securityLevel: distributedKVStore.SecurityLevel.S1
     };
     kvManager.getKVStore('storeId', options, function (err, store) {
       if (err) {
         console.error(`Failed to get KVStore: code is ${err.code},message is ${err.message}`);
         return;
       }
       console.log('Obtained KVStore successfully');
       kvStore = store;
     });
   } catch (e) {
     console.error(`An unexpected error occurred.code is ${e.code},message is ${e.message}`);
   }

NOTE

For data synchronization between networked devices, you are advised to open the distributed KV store during application startup to obtain the database handle. With this database handle (kvStore in this example), you can perform operations, such as inserting data into the KV store, without creating the KV store repeatedly during the lifecycle of the handle.

  1. Subscribe to changes in the distributed data.

The following is the sample code for subscribing to the data changes of a single KV store:

   try{
       kvStore.on('dataChange', distributedKVStore.SubscribeType.SUBSCRIBE_TYPE_ALL, function (data) {
           console.log(`dataChange callback call data: ${data}`);
       });
   }catch(e){
       console.error(`An unexpected error occured.code is ${e.code},message is ${e.message}`);
   }
  1. Write data to the single KV store.

    1. Construct the Key and Value to be written into the single KV store.
    2. Write key-value pairs into the single KV store.

The following is the sample code for writing key-value pairs of the string type into the single KV store:

   const KEY_TEST_STRING_ELEMENT = 'key_test_string';
   const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
   try {
       kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err,data) {
           if (err != undefined) {
               console.error(`Failed to put.code is ${err.code},message is ${err.message}`);
               return;
           }
           console.log('Put data successfully');
       });
   }catch (e) {
       console.error(`An unexpected error occurred.code is ${e.code},message is ${e.message}`);
   }
  1. Query data in the single KV store.

    1. Construct the Key to be queried from the single KV store.
    2. Query data from the single KV store.

The following is the sample code for querying data of the string type from the single KV store:

   const KEY_TEST_STRING_ELEMENT = 'key_test_string';
   const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
   try {
       kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err,data) {
           if (err != undefined) {
               console.error(`Failed to put.code is ${err.code},message is ${err.message}`);
               return;
           }
           console.log('Put data successfully');
           kvStore.get(KEY_TEST_STRING_ELEMENT, function (err,data) {
           if (err != undefined) {
               console.error(`Failed to get data.code is ${err.code},message is ${err.message}`);
               return;
           }
               console.log(`Obtained data successfully:${data}`);
           });
       });
   }catch (e) {
       console.error(`Failed to get.code is ${e.code},message is ${e.message}`);
   }
  1. Synchronize data to other devices.

Select the devices in the same network and the synchronization mode to synchronize data.

NOTE

The APIs of the deviceManager module are system interfaces.

The following is the example code for synchronizing data in a single KV store:

   import deviceManager from '@ohos.distributedHardware.deviceManager';
   
   let devManager;
   // Create deviceManager.
   deviceManager.createDeviceManager('bundleName', (err, value) => {
       if (!err) {
           devManager = value;
           // deviceIds is obtained by deviceManager by calling getTrustedDeviceListSync().
           let deviceIds = [];
           if (devManager != null) {
               var devices = devManager.getTrustedDeviceListSync();
               for (var i = 0; i < devices.length; i++) {
                   deviceIds[i] = devices[i].deviceId;
               }
           }
           try{
               // 1000 indicates that the maximum delay is 1000 ms.
               kvStore.sync(deviceIds, distributedKVStore.SyncMode.PUSH_ONLY, 1000);
           } catch (e) {
                console.error(`An unexpected error occurred. code is ${e.code},message is ${e.message}`);
           }
       }
   });

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Data Management

harmony 鸿蒙DataShare Development

harmony 鸿蒙DataShare Overview

harmony 鸿蒙Distributed Data Object Development

harmony 鸿蒙Distributed Data Object Overview

harmony 鸿蒙Distributed Data Service Overview

harmony 鸿蒙Preferences Development

harmony 鸿蒙Preferences Overview

harmony 鸿蒙RDB Development

harmony 鸿蒙RDB Overview

0  赞