1# @ohos.data.cloudData (端云协同) 2 3端云协同提供结构化数据(RDB Store)端云同步的能力。即:云作为数据的中心节点,通过与云的数据同步,实现数据云备份、同帐号设备间的数据一致性。 4 5该模块提供以下端云协同相关的常用功能: 6 7- [Config](#config):提供配置端云协同的方法,包括云同步打开、关闭、清理数据、数据变化通知。 8 9> **说明:** 10> 11> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12> 13> - 本模块接口为系统接口。 14 15## 导入模块 16 17```ts 18import cloudData from '@ohos.data.cloudData'; 19``` 20 21## ClearAction 22 23清除本地下载的云端数据的行为枚举。 24 25**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Config 26 27| 名称 | 说明 | 28| --------- | ---------------------------- | 29| CLEAR_CLOUD_INFO | 清除从云端下载的数据的云标识,相关数据作为本地数据保存。 | 30| CLEAR_CLOUD_DATA_AND_INFO |清除从云端下载的数据,不包括本地已修改的云端数据。 | 31 32## Config 33 34提供配置端云协同的方法,包括云同步打开、关闭、清理数据、数据变化通知。 35 36### enableCloud 37 38static enableCloud(accountId: string, switches: { [bundleName: string]: boolean }, callback: AsyncCallback<void>): void 39 40打开端云协同,使用callback异步回调。 41 42**需要权限**:ohos.permission.CLOUDDATA_CONFIG 43 44**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Config 45 46**参数:** 47 48| 参数名 | 类型 | 必填 | 说明 | 49| --------- | ------------------------------- | ---- | ------------------------------------------------------------ | 50| accountId | string | 是 | 具体打开的云帐号ID。 | 51| switches | {[bundleName: string]: boolean} | 是 | 各应用的端云协同开关信息,true为打开该应用端云开关,false为关闭该应用端云开关。 | 52| callback | AsyncCallback<void> | 是 | 回调函数。 | 53 54**示例:** 55 56```ts 57import { BusinessError } from '@ohos.base'; 58 59let account = 'test_id'; 60let switches: Record<string, boolean> = { 'test_bundleName1': true, 'test_bundleName2': false }; 61try { 62 cloudData.Config.enableCloud(account, switches, (err) => { 63 if (err === undefined) { 64 console.info('Succeeded in enabling cloud'); 65 } else { 66 console.error(`Failed to enable.Code: ${err.code}, message: ${err.message}`); 67 } 68 }); 69} catch (e) { 70 let error = e as BusinessError; 71 console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); 72} 73``` 74 75### enableCloud 76 77static enableCloud(accountId: string, switches: { [bundleName: string]: boolean }): Promise<void> 78 79打开端云协同,使用Promise异步回调。 80 81**需要权限**:ohos.permission.CLOUDDATA_CONFIG 82 83**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Config 84 85**参数:** 86 87| 参数名 | 类型 | 必填 | 说明 | 88| --------- | ------------------------------- | ---- | ------------------------------------------------------------ | 89| accountId | string | 是 | 具体打开的云帐号ID。 | 90| switches | {[bundleName: string]: boolean} | 是 | 各应用的端云协同开关信息,true为打开该应用端云开关,false为关闭该应用端云开关。 | 91 92**返回值:** 93 94| 类型 | 说明 | 95| ------------------- | ------------------------- | 96| Promise<void> | 无返回结果的Promise对象。 | 97 98**示例:** 99 100```ts 101import { BusinessError } from '@ohos.base'; 102 103let account = 'test_id'; 104let switches: Record<string, boolean> = { 'test_bundleName1': true, 'test_bundleName2': false }; 105try { 106 cloudData.Config.enableCloud(account, switches).then(() => { 107 console.info('Succeeded in enabling cloud'); 108 }).catch((err: BusinessError) => { 109 console.error(`Failed to enable.Code: ${err.code}, message: ${err.message}`); 110 }); 111} catch (e) { 112 let error = e as BusinessError; 113 console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); 114} 115``` 116 117### disableCloud 118 119static disableCloud(accountId: string, callback: AsyncCallback<void>): void 120 121关闭端云协同,使用callback异步回调。 122 123**需要权限**:ohos.permission.CLOUDDATA_CONFIG 124 125**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Config 126 127**参数:** 128 129| 参数名 | 类型 | 必填 | 说明 | 130| --------- | ------------------------- | ---- | -------------------- | 131| accountId | string | 是 | 具体打开的云帐号ID。 | 132| callback | AsyncCallback<void> | 是 | 回调函数。 | 133 134**示例:** 135 136```ts 137import { BusinessError } from '@ohos.base'; 138 139let account = 'test_id'; 140try { 141 cloudData.Config.disableCloud(account, (err) => { 142 if (err === undefined) { 143 console.info('Succeeded in disabling cloud'); 144 } else { 145 console.error(`Failed to disableCloud. Code: ${err.code}, message: ${err.message}`); 146 } 147 }); 148} catch (e) { 149 let error = e as BusinessError; 150 console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); 151} 152``` 153 154### disableCloud 155 156static disableCloud(accountId: string): Promise<void> 157 158关闭端云协同,使用Promise异步回调。 159 160**需要权限**:ohos.permission.CLOUDDATA_CONFIG 161 162**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Config 163 164**参数:** 165 166| 参数名 | 类型 | 必填 | 说明 | 167| --------- | ------ | ---- | -------------------- | 168| accountId | string | 是 | 具体打开的云帐号ID。 | 169 170**返回值:** 171 172| 类型 | 说明 | 173| ------------------- | ------------------------- | 174| Promise<void> | 无返回结果的Promise对象。 | 175 176**示例:** 177 178```ts 179import { BusinessError } from '@ohos.base'; 180 181let account = 'test_id'; 182try { 183 cloudData.Config.disableCloud(account).then(() => { 184 console.info('Succeeded in disabling cloud'); 185 }).catch((err: BusinessError) => { 186 console.error(`Failed to disableCloud. Code: ${err.code}, message: ${err.message}`); 187 }); 188} catch (e) { 189 let error = e as BusinessError; 190 console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); 191} 192``` 193 194### changeAppCloudSwitch 195 196static changeAppCloudSwitch(accountId: string, bundleName: string, status: boolean, callback: AsyncCallback<void>): void 197 198修改单个应用端云协同开关,使用callback异步回调。 199 200**需要权限**:ohos.permission.CLOUDDATA_CONFIG 201 202**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Config 203 204**参数:** 205 206| 参数名 | 类型 | 必填 | 说明 | 207| --------- | ------------------------------- | ---- | ---------------------------- | 208| accountId | string | 是 | 具体打开的云帐号ID。 | 209| bundleName| string | 是 | 应用名。 | 210| status | boolean | 是 | 应用的端云协同开关信息,true为打开该应用端云开关,false为关闭该应用端云开关。 | 211| callback | AsyncCallback<void> | 是 | 回调函数。 | 212 213**示例:** 214 215```ts 216import { BusinessError } from '@ohos.base'; 217 218let account = 'test_id'; 219let bundleName = 'test_bundleName'; 220try { 221 cloudData.Config.changeAppCloudSwitch(account, bundleName, true, (err) => { 222 if (err === undefined) { 223 console.info('Succeeded in changing App cloud switch'); 224 } else { 225 console.error(`Failed to change App cloud switch. Code: ${err.code}, message: ${err.message}`); 226 } 227 }); 228} catch (e) { 229 let error = e as BusinessError; 230 console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); 231} 232``` 233 234### changeAppCloudSwitch 235 236static changeAppCloudSwitch(accountId: string, bundleName: string, status: boolean): Promise<void> 237 238修改单个应用端云协同开关,使用Promise异步回调。 239 240**需要权限**:ohos.permission.CLOUDDATA_CONFIG 241 242**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Config 243 244**参数:** 245 246| 参数名 | 类型 | 必填 | 说明 | 247| --------- | ------------------------------- | ---- | ---------------------------- | 248| accountId | string | 是 | 具体打开的云帐号ID。 | 249| bundleName| string | 是 | 应用名。 | 250| status | boolean | 是 | 应用的端云协同开关信息,true为打开该应用端云开关,false为关闭该应用端云开关。 | 251 252**返回值:** 253 254| 类型 | 说明 | 255| ------------------- | ------------------------- | 256| Promise<void> | 无返回结果的Promise对象。 | 257 258**示例:** 259 260```ts 261import { BusinessError } from '@ohos.base'; 262 263let account = 'test_id'; 264let bundleName = 'test_bundleName'; 265try { 266 cloudData.Config.changeAppCloudSwitch(account, bundleName, true).then(() => { 267 console.info('Succeeded in changing App cloud switch'); 268 }).catch((err: BusinessError) => { 269 console.error(`Failed to change App cloud switch. Code is ${err.code}, message is ${err.message}`); 270 }); 271} catch (e) { 272 let error = e as BusinessError; 273 console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); 274} 275``` 276 277### notifyDataChange 278 279static notifyDataChange(accountId: string, bundleName: string, callback: AsyncCallback<void>): void 280 281通知云端的数据变更,使用callback异步回调。 282 283**需要权限**:ohos.permission.CLOUDDATA_CONFIG 284 285**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Server 286 287**参数:** 288 289| 参数名 | 类型 | 必填 | 说明 | 290| ---------- | ------------------------- | ---- | -------------------- | 291| accountId | string | 是 | 具体打开的云帐号ID。 | 292| bundleName | string | 是 | 应用名。 | 293| callback | AsyncCallback<void> | 是 | 回调函数。 | 294 295**示例:** 296 297```ts 298import { BusinessError } from '@ohos.base'; 299 300let account = 'test_id'; 301let bundleName = 'test_bundleName'; 302try { 303 cloudData.Config.notifyDataChange(account, bundleName, (err) => { 304 if (err === undefined) { 305 console.info('Succeeded in notifying the change of data'); 306 } else { 307 console.error(`Failed to notify the change of data. Code: ${err.code}, message: ${err.message}`); 308 } 309 }); 310} catch (e) { 311 let error = e as BusinessError; 312 console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); 313} 314``` 315 316### notifyDataChange 317 318static notifyDataChange(accountId: string,bundleName: string): Promise<void> 319 320通知云端的数据变更,使用Promise异步回调。 321 322**需要权限**:ohos.permission.CLOUDDATA_CONFIG 323 324**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Server 325 326**参数:** 327 328| 参数名 | 类型 | 必填 | 说明 | 329| ---------- | ------ | ---- | -------------------- | 330| accountId | string | 是 | 具体打开的云帐号ID。 | 331| bundleName | string | 是 | 应用名。 | 332 333**返回值:** 334 335| 类型 | 说明 | 336| ------------------- | ------------------------- | 337| Promise<void> | 无返回结果的Promise对象。 | 338 339**示例:** 340 341```ts 342import { BusinessError } from '@ohos.base'; 343 344let account = 'test_id'; 345let bundleName = 'test_bundleName'; 346try { 347 cloudData.Config.notifyDataChange(account, bundleName).then(() => { 348 console.info('Succeeded in notifying the change of data'); 349 }).catch((err: BusinessError) => { 350 console.error(`Failed to notify the change of data. Code: ${err.code}, message: ${err.message}`); 351 }); 352} catch (e) { 353 let error = e as BusinessError; 354 console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); 355} 356``` 357 358### clear 359 360static clear(accountId: string, appActions: { [bundleName: string]: ClearAction }, callback: AsyncCallback<void>): void 361 362清除本地下载的云端数据,使用callback异步回调。 363 364**需要权限**:ohos.permission.CLOUDDATA_CONFIG 365 366**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Config 367 368**参数:** 369 370| 参数名 | 类型 | 必填 | 说明 | 371| ---------- | --------------------------------------------------- | ---- | -------------------------------- | 372| accountId | string | 是 | 具体打开的云帐号ID。 | 373| appActions | {[bundleName: string]: [ClearAction](#clearaction)} | 是 | 要清除数据的应用信息及清除规则。 | 374| callback | AsyncCallback<void> | 是 | 回调函数。 | 375 376**示例:** 377 378```ts 379import { BusinessError } from '@ohos.base'; 380 381let account = "test_id"; 382type dataType = Record<string, cloudData.ClearAction> 383let appActions: dataType = { 384 'test_bundleName1': cloudData.ClearAction.CLEAR_CLOUD_INFO, 385 'test_bundleName2': cloudData.ClearAction.CLEAR_CLOUD_DATA_AND_INFO 386}; 387try { 388 cloudData.Config.clear(account, appActions, (err) => { 389 if (err === undefined) { 390 console.info('Succeeding in clearing cloud data'); 391 } else { 392 console.error(`Failed to clear cloud data. Code: ${err.code}, message: ${err.message}`); 393 } 394 }); 395} catch (e) { 396 let error = e as BusinessError; 397 console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); 398} 399``` 400 401### clear 402 403static clear(accountId: string, appActions: { [bundleName: string]: ClearAction }): Promise<void> 404 405清除本地下载的云端数据,使用Promise异步回调。 406 407**需要权限**:ohos.permission.CLOUDDATA_CONFIG 408 409**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Config 410 411**参数:** 412 413| 参数名 | 类型 | 必填 | 说明 | 414| ---------- | --------------------------------------------------- | ---- | -------------------------------- | 415| accountId | string | 是 | 具体打开的云帐号ID。 | 416| appActions | {[bundleName: string]: [ClearAction](#clearaction)} | 是 | 要清除数据的应用信息及清除规则。 | 417 418**返回值:** 419 420| 类型 | 说明 | 421| ------------------- | ------------------------- | 422| Promise<void> | 无返回结果的Promise对象。 | 423 424**示例:** 425 426```ts 427import { BusinessError } from '@ohos.base'; 428 429let account = "test_id"; 430type dataType = Record<string, cloudData.ClearAction>; 431let appActions: dataType = { 432 'test_bundleName1': cloudData.ClearAction.CLEAR_CLOUD_INFO, 433 'test_bundleName2': cloudData.ClearAction.CLEAR_CLOUD_DATA_AND_INFO 434}; 435try { 436 cloudData.Config.clear(account, appActions).then(() => { 437 console.info('Succeeding in clearing cloud data'); 438 }).catch((err: BusinessError) => { 439 console.error(`Failed to clear cloud data. Code: ${err.code}, message: ${err.message}`); 440 }); 441} catch (e) { 442 let error = e as BusinessError; 443 console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); 444} 445``` 446