harmony 鸿蒙@ohos.data.cloudData (Device-Cloud Synergy)

2023-06-24 浏览 (521)

@ohos.data.cloudData (Device-Cloud Synergy)

The cloudData module provides the capability of synchronizing the structured data (in RDB stores) between the device and cloud. The cloud serves as the central node of data. The devices synchronize data with the data in the cloud to implement cloud data backup and data consistency between the devices with the same account.

This module provides the following common functions:

  • Config: provides methods for configuring device-cloud synergy, including enabling and disabling cloud synchronization, clearing data, and notifying data changes.

NOTE

  • The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs provided by this module are system APIs.

Modules to Import

import cloudData from '@ohos.data.cloudData';

ClearAction

Enumerates the actions to take to clear the downloaded cloud data locally.

System capability: SystemCapability.DistributedDataManager.CloudSync.Config

NameDescription
CLEAR_CLOUD_INFOClear only the cloud identifier of the data downloaded from the cloud. The related data is still stored as local data.
CLEAR_CLOUD_DATA_AND_INFOClear the data downloaded from the cloud, excluding the cloud data that has been modified locally.

Config

Provides APIs for implementing device-cloud synergy, including enabling and disabling device-cloud synchronization, clearing data, and notifying data changes.

enableCloud

static enableCloud(accountId: string, switches: {[bundleName: string]: boolean}, callback: AsyncCallback<void>):void

Enables device-cloud synergy. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLOUDDATA_CONFIG

System capability: SystemCapability.DistributedDataManager.CloudSync.Config

Parameters

NameTypeMandatoryDescription
accountIdstringYesID of the cloud account.
switches{[bundleName: string]: boolean}YesDevice-cloud synergy switches for applications. The value true means to enable the device-cloud synergy; the value false means the opposite.
callbackAsyncCallback<void>YesCallback invoked to return the result.

Example

import { BusinessError } from '@ohos.base';

let account = 'test_id';
let switches: Record<string, boolean> = { 'test_bundleName1': true, 'test_bundleName2': false };
try {
  cloudData.Config.enableCloud(account, switches, (err) => {
    if (err === undefined) {
      console.info('Succeeded in enabling cloud');
    } else {
      console.error(`Failed to enable.Code: ${err.code}, message: ${err.message}`);
    }
  });
} catch (e) {
  let error = e as BusinessError;
  console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}

enableCloud

static enableCloud(accountId: string, switches: {[bundleName: string]: boolean}): Promise<void>

Enables device-cloud synergy. This API uses a promise to return the result.

Required permissions: ohos.permission.CLOUDDATA_CONFIG

System capability: SystemCapability.DistributedDataManager.CloudSync.Config

Parameters

NameTypeMandatoryDescription
accountIdstringYesID of the cloud account.
switches{[bundleName: string]: boolean}YesDevice-cloud synergy switches for applications. The value true means to enable the device-cloud synergy; the value false means the opposite.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { BusinessError } from '@ohos.base';

let account = 'test_id';
let switches: Record<string, boolean> = { 'test_bundleName1': true, 'test_bundleName2': false };
try {
  cloudData.Config.enableCloud(account, switches).then(() => {
    console.info('Succeeded in enabling cloud');
  }).catch((err: BusinessError) => {
    console.error(`Failed to enable.Code: ${err.code}, message: ${err.message}`);
  });
} catch (e) {
  let error = e as BusinessError;
  console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}

disableCloud

static disableCloud(accountId: string, callback: AsyncCallback<void>):void

Disables device-cloud synergy. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLOUDDATA_CONFIG

System capability: SystemCapability.DistributedDataManager.CloudSync.Config

Parameters

NameTypeMandatoryDescription
accountIdstringYesID of the cloud account.
callbackAsyncCallback<void>YesCallback invoked to return the result.

Example

import { BusinessError } from '@ohos.base';

let account = 'test_id';
try {
  cloudData.Config.disableCloud(account, (err) => {
    if (err === undefined) {
      console.info('Succeeded in disabling cloud');
    } else {
      console.error(`Failed to disableCloud. Code: ${err.code}, message: ${err.message}`);
    }
  });
} catch (e) {
  let error = e as BusinessError;
  console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}

disableCloud

static disableCloud(accountId: string): Promise<void>

Disables device-cloud synergy. This API uses a promise to return the result.

Required permissions: ohos.permission.CLOUDDATA_CONFIG

System capability: SystemCapability.DistributedDataManager.CloudSync.Config

Parameters

NameTypeMandatoryDescription
accountIdstringYesID of the cloud account.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { BusinessError } from '@ohos.base';

let account = 'test_id';
try {
  cloudData.Config.disableCloud(account).then(() => {
    console.info('Succeeded in disabling cloud');
  }).catch((err: BusinessError) => {
    console.error(`Failed to disableCloud. Code: ${err.code}, message: ${err.message}`);
  });
} catch (e) {
  let error = e as BusinessError;
  console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}

changeAppCloudSwitch

static changeAppCloudSwitch(accountId: string,bundleName:string,status:boolean, callback: AsyncCallback<void>):void

Changes the device-cloud synergy switch for an application. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLOUDDATA_CONFIG

System capability: SystemCapability.DistributedDataManager.CloudSync.Config

Parameters

NameTypeMandatoryDescription
accountIdstringYesID of the cloud account.
bundleNamestringYesBundle name of the application.
statusbooleanYesSetting of the device-cloud synergy switch for the application. The value true means to enable the device-cloud synergy; the value false means the opposite.
callbackAsyncCallback<void>YesCallback invoked to return the result.

Example

import { BusinessError } from '@ohos.base';

let account = 'test_id';
let bundleName = 'test_bundleName';
try {
  cloudData.Config.changeAppCloudSwitch(account, bundleName, true, (err) => {
    if (err === undefined) {
      console.info('Succeeded in changing App cloud switch');
    } else {
      console.error(`Failed to change App cloud switch. Code: ${err.code}, message: ${err.message}`);
    }
  });
} catch (e) {
  let error = e as BusinessError;
  console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}

changeAppCloudSwitch

static changeAppCloudSwitch(accountId: string,bundleName:string,status:boolean): Promise<void>

Changes the device-cloud synergy switch for an application. This API uses a promise to return the result.

Required permissions: ohos.permission.CLOUDDATA_CONFIG

System capability: SystemCapability.DistributedDataManager.CloudSync.Config

Parameters

NameTypeMandatoryDescription
accountIdstringYesID of the cloud account.
bundleNamestringYesBundle name of the application.
statusbooleanYesSetting of the device-cloud synergy switch for the application. The value true means to enable the device-cloud synergy; the value false means the opposite.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { BusinessError } from '@ohos.base';

let account = 'test_id';
let bundleName = 'test_bundleName';
try {
  cloudData.Config.changeAppCloudSwitch(account, bundleName, true).then(() => {
    console.info('Succeeded in changing App cloud switch');
  }).catch((err: BusinessError) => {
    console.error(`Failed to change App cloud switch. Code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  let error = e as BusinessError;
  console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}

notifyDataChange

static notifyDataChange(accountId: string,bundleName:string, callback: AsyncCallback<void>):void

Notifies the data changes in the cloud. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLOUDDATA_CONFIG

System capability: SystemCapability.DistributedDataManager.CloudSync.Server

Parameters

NameTypeMandatoryDescription
accountIdstringYesID of the cloud account.
bundleNamestringYesBundle name of the application.
callbackAsyncCallback<void>YesCallback invoked to return the result.

Example

import { BusinessError } from '@ohos.base';

let account = 'test_id';
let bundleName = 'test_bundleName';
try {
  cloudData.Config.notifyDataChange(account, bundleName, (err) => {
    if (err === undefined) {
      console.info('Succeeded in notifying the change of data');
    } else {
      console.error(`Failed to notify the change of data. Code: ${err.code}, message: ${err.message}`);
    }
  });
} catch (e) {
  let error = e as BusinessError;
  console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}

notifyDataChange

static notifyDataChange(accountId: string,bundleName:string): Promise<void>

Notifies the data changes in the cloud. This API uses a promise to return the result.

Required permissions: ohos.permission.CLOUDDATA_CONFIG

System capability: SystemCapability.DistributedDataManager.CloudSync.Server

Parameters

NameTypeMandatoryDescription
accountIdstringYesID of the cloud account.
bundleNamestringYesBundle name of the application.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { BusinessError } from '@ohos.base';

let account = 'test_id';
let bundleName = 'test_bundleName';
try {
  cloudData.Config.notifyDataChange(account, bundleName).then(() => {
    console.info('Succeeded in notifying the change of data');
  }).catch((err: BusinessError) => {
    console.error(`Failed to notify the change of data. Code: ${err.code}, message: ${err.message}`);
  });
} catch (e) {
  let error = e as BusinessError;
  console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}

clear

static clear(accountId: string, appActions: {[bundleName: string]: ClearAction}, callback: AsyncCallback<void>):void

Clears the cloud data locally. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLOUDDATA_CONFIG

System capability: SystemCapability.DistributedDataManager.CloudSync.Config

Parameters

NameTypeMandatoryDescription
accountIdstringYesID of the cloud account.
appActions{[bundleName: string]: ClearAction}YesInformation about the application whose data is to be cleared and the action to take.
callbackAsyncCallback<void>YesCallback invoked to return the result.

Example

import { BusinessError } from '@ohos.base';

let action = cloudData.ClearAction;
let account = "test_id";
type dataType = Record<string, cloudData.ClearAction>
let appActions: dataType = {
  'test_bundleName1': action.CLEAR_CLOUD_INFO,
  'test_bundleName2': action.CLEAR_CLOUD_DATA_AND_INFO
};
try {
  cloudData.Config.clear(account, appActions, (err) => {
    if (err === undefined) {
      console.info('Succeeding in clearing cloud data');
    } else {
      console.error(`Failed to clear cloud data. Code: ${err.code}, message: ${err.message}`);
    }
  });
} catch (e) {
  let error = e as BusinessError;
  console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}

clear

static clear(accountId: string, appActions: {[bundleName: string]: ClearAction}): Promise<void>

Clears the cloud data locally. This API uses a promise to return the result.

Required permissions: ohos.permission.CLOUDDATA_CONFIG

System capability: SystemCapability.DistributedDataManager.CloudSync.Config

Parameters

NameTypeMandatoryDescription
accountIdstringYesID of the cloud account.
appActions{[bundleName: string]: ClearAction}YesInformation about the application whose data is to be cleared and the action to take.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { BusinessError } from '@ohos.base';

let action = cloudData.ClearAction;
let account = "test_id";
type dataType = Record<string, cloudData.ClearAction>;
let appActions: dataType = {
  'test_bundleName1': action.CLEAR_CLOUD_INFO,
  'test_bundleName2': action.CLEAR_CLOUD_DATA_AND_INFO
};
try {
  cloudData.Config.clear(account, appActions).then(() => {
    console.info('Succeeding in clearing cloud data');
  }).catch((err: BusinessError) => {
    console.error(`Failed to clear cloud data. Code: ${err.code}, message: ${err.message}`);
  });
} catch (e) {
  let error = e as BusinessError;
  console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/212f4a25f0de49e7943346e038769588