1e41f4b71Sopenharmony_ci# @ohos.data.cloudData (端云服务) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci端云服务提供端云协同、端云共享和端云策略。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci端云协同提供结构化数据(RDB Store)端云同步的能力。即:云作为数据的中心节点,通过与云的数据同步,实现数据云备份、同账号设备间的数据一致性。 6e41f4b71Sopenharmony_ci端云配置提供端云同步策略配置的能力。 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci> **说明:** 9e41f4b71Sopenharmony_ci> 10e41f4b71Sopenharmony_ci> - 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## 导入模块 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci```ts 15e41f4b71Sopenharmony_ciimport { cloudData } from '@kit.ArkData'; 16e41f4b71Sopenharmony_ci``` 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## StrategyType 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci云同步策略类型枚举。 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 25e41f4b71Sopenharmony_ci| --------- |---|-----------| 26e41f4b71Sopenharmony_ci| NETWORK | 0 | 通过网络同步策略。 | 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci## NetWorkStrategy 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci网络策略参数枚举。 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 35e41f4b71Sopenharmony_ci| --------- |---|-----------| 36e41f4b71Sopenharmony_ci| WIFI | 1 | WIFI网络策略。 | 37e41f4b71Sopenharmony_ci| CELLULAR | 2 | 蜂窝网络策略。 | 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci## cloudData.setCloudStrategy 40e41f4b71Sopenharmony_cisetCloudStrategy(strategy: StrategyType, param?: Array<commonType.ValueType>): Promise<void> 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci<!--RP1--> 43e41f4b71Sopenharmony_ci设置应用自身的云同步策略,若未设置,则执行全局策略[setGlobalCloudStrategy<sup>12+</sup>](js-apis-data-cloudData-sys.md#setglobalcloudstrategy12),全局策略若未设置,默认使用WIFI和蜂窝策略。使用Promise异步回调。<!--RP1End--> 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 48e41f4b71Sopenharmony_ci| ---------- |-----------------------------------------------------------------------------| ---- | -------------------------------- | 49e41f4b71Sopenharmony_ci| strategy | [StrategyType](#strategytype) | 是 | 配置的策略类型。 | 50e41f4b71Sopenharmony_ci| param | Array<[commonType.ValueType](js-apis-data-commonType.md#valuetype)> | 否 | 策略参数。不填写取消所有配置。 | 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci**返回值:** 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci| 类型 | 说明 | 55e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 56e41f4b71Sopenharmony_ci| Promise<void> | 无返回结果的Promise对象。 | 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci**错误码:** 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 63e41f4b71Sopenharmony_ci|-----------| ------------------------------------------------------------ | 64e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 65e41f4b71Sopenharmony_ci| 801 | Capability not supported.| 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**样例:** 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci```ts 70e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci// 仅WIFI同步 73e41f4b71Sopenharmony_cicloudData.setCloudStrategy(cloudData.StrategyType.NETWORK, [cloudData.NetWorkStrategy.WIFI]).then(() => { 74e41f4b71Sopenharmony_ci console.info('Succeeded in setting the cloud strategy'); 75e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 76e41f4b71Sopenharmony_ci console.error(`Failed to set cloud strategy. Code: ${err.code}, message: ${err.message}`); 77e41f4b71Sopenharmony_ci}); 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci``` 80e41f4b71Sopenharmony_ci<!--no_check--> 81