9f3a45c6创建于 2025年12月19日历史提交

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

The cloudData module provides APIs for implementing device-cloud synergy and device-cloud sharing, and setting the device-cloud sync strategy.

Device-cloud synergy enables sync of the structured data (in RDB stores) between devices and the cloud. The cloud serves as a data hub to implement data backup in the cloud and data consistency between the devices with the same account. This module also provides the capability of setting the device-cloud sync strategy.

NOTE

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

Modules to Import

import { cloudData } from '@kit.ArkData';

StrategyType

Enumerates the types of the cloud-device sync strategy.

System capability: SystemCapability.DistributedDataManager.CloudSync.Client

Name Value Description
NETWORK 0 Sync over the network.

NetWorkStrategy

Enumerates the network sync options.

System capability: SystemCapability.DistributedDataManager.CloudSync.Client

Name Value Description
WIFI 1 Sync over Wi-Fi.
CELLULAR 2 Sync over the cellular network.

cloudData.setCloudStrategy

setCloudStrategy(strategy: StrategyType, param?: Array<commonType.ValueType>): Promise<void>

Sets the cloud sync strategy of an application. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.CloudSync.Client

Parameters

Name Type Mandatory Description
strategy StrategyType Yes Type of the strategy to set.
param Array<commonType.ValueType> No Strategy parameters to set. Currently, only network strategies can be set. By default, Wi-Fi and cellular network are supported.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801 Capability not supported.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Sync data over Wi-Fi only.
cloudData.setCloudStrategy(cloudData.StrategyType.NETWORK, [cloudData.NetWorkStrategy.WIFI]).then(() => {
    console.info('Succeeded in setting the cloud strategy');
}).catch((err: BusinessError) => {
    console.error(`Failed to set cloud strategy. Code: ${err.code}, message: ${err.message}`);
});