1# Syncing Assets (Backup and Restore) (ArkTS) 2 3## Adding Assets That Support Sync 4 5Add an asset with the password **demo_pwd**, alias **demo_alias**, and additional information **demo_label**. 6 7```typescript 8import { asset } from '@kit.AssetStoreKit'; 9import { util } from '@kit.ArkTS'; 10import { BusinessError } from '@kit.BasicServicesKit'; 11 12function stringToArray(str: string): Uint8Array { 13 let textEncoder = new util.TextEncoder(); 14 return textEncoder.encodeInto(str); 15} 16 17let attr: asset.AssetMap = new Map(); 18attr.set(asset.Tag.SECRET, stringToArray('demo_pwd')); 19attr.set(asset.Tag.ALIAS, stringToArray('demo_alias')); 20attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); 21attr.set(asset.Tag.SYNC_TYPE, asset.SyncType.TRUSTED_DEVICE); // You need to specify the sync type between trusted devices (for example, clone between old and new devices). 22try { 23 asset.add(attr).then(() => { 24 console.info(`Asset added with sync successfully.`); 25 }).catch((err: BusinessError) => { 26 console.error(`Failed to add Asset with sync. Code is ${err.code}, message is ${err.message}`); 27 }) 28} catch (error) { 29 let err = error as BusinessError; 30 console.error(`Failed to add Asset with sync. Code is ${err.code}, message is ${err.message}`); 31} 32``` 33 34## Accessing the Backup and Restore Extension Capability 35 36To trigger the backup and restore of application data, see [Accessing Backup and Restore](../../file-management/app-file-backup-extension.md). 37 38## Querying the Sync Result of Assets 39 40### Available APIs 41 42You can call [asset.querySyncResult](../../reference/apis-asset-store-kit/js-apis-asset.md#assetquerysyncresult20) to query the sync result of assets. For more information, see the API reference. 43 44The following table describes the attributes of **AssetMap** for querying the sync result of assets. 45 46| Attribute Name (Tag) | Attribute Content (Asset_Value) | Mandatory/Optional | Description | 47| --------------------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ | 48| REQUIRE_ATTR_ENCRYPTED<sup>14+</sup> | Type: Boolean| Yes| Whether to query the sync result of the asset whose custom additional information is encrypted. The value **true** means to query the sync result; the value **false** means the opposite. The default value is **false**.| 49| GROUP_ID<sup>18+</sup> | Type: Uint8Array<br>Length: 7-127 bytes| Yes| Group to which the asset to be queried belongs. By default, this parameter is not specified.| 50 51### Sample Code 52 53```typescript 54import { asset } from '@kit.AssetStoreKit'; 55import { BusinessError } from '@kit.BasicServicesKit'; 56 57let query: asset.AssetMap = new Map(); 58asset.querySyncResult(query).then((res: asset.SyncResult) => { 59 console.info(`sync result: ${JSON.stringify(res)}`); 60}).catch ((err: BusinessError) => { 61 console.error(`Failed to query sync result of Asset. Code is ${err.code}, message is ${err.message}`); 62}); 63``` 64 65## Notes and Constraints 66 67For a successful sync between trusted devices, the assets of both old and new devices must be accessible. Otherwise, the sync might fail. 68 69* For assets that are accessible only when a password is set, the sync will fail if no lock screen password is set on either the old or new device. 70 71* For assets that are accessible only when the screen is unlocked, the sync will fail if the screen of either the old or new device is locked. 72 73* For assets that are accessible only after user authentication, the sync will fail if no lock screen password is set on the old device. 74