openharmony 鸿蒙 asset-js-batch-update

2026-08-25 浏览 (1)

Updating Assets in Batches (ArkTS)

Available APIs

Since version 26.0.0, the system provides the asynchronous API batchUpdate for you to update assets in batches.

The following table describes the attributes of AssetMap for updating assets in batches.

NOTE

In the following table, the attributes ALIAS and those starting with DATA_LABEL are custom asset attributes reserved for services. These attributes are not encrypted. Therefore, do not put sensitive personal data in these attributes.

  • Attributes in sourceAttributes:
Attribute Name (Tag)ValueMandatoryDescription
ALIASType: Uint8Array
Length: 1-256 bytes
YesAsset alias, which uniquely identifies an asset.
ACCESSIBILITYType: number
Value range: see Accessibility
NoAccess control based on the lock screen status.
REQUIRE_PASSWORD_SETType: BooleanNoWhether the asset is accessible only when a lock screen password is set. The value true means the asset is accessible only when a lock screen password is set. The value false means that the asset can be accessed regardless of whether a lock screen password is set.
AUTH_TYPEType: number
Value range: see AuthType
NoType of user authentication required for accessing the asset.
SYNC_TYPEType: number
Value range: see SyncType
NoType of sync supported by the asset.
IS_PERSISTENTType: BooleanNoWhether to retain the asset when the application is uninstalled. The value true means to retain the asset even after the application is uninstalled. The value false means the opposite.
DATA_LABEL_CRITICAL_1Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service with integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_CRITICAL_2Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service with integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_CRITICAL_3Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service with integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_CRITICAL_4Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service with integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_1Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_2Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_3Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_4Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_LOCAL_112+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
DATA_LABEL_NORMAL_LOCAL_212+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
DATA_LABEL_NORMAL_LOCAL_312+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
DATA_LABEL_NORMAL_LOCAL_412+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
REQUIRE_ATTR_ENCRYPTED14+Type: BooleanNoWhether to update the encrypted data of service customized supplementary information. The value true means to update the encrypted data of service customized supplementary information; the value false means to update the non-encrypted data of service customized supplementary information. The default value is false.
GROUP_ID18+Type: Uint8Array
Length: 7-127 bytes
NoGroup to which the asset to be updated belongs. By default, this parameter is not specified.
  • Attributes in destAttributes:
Attribute Name (Tag)ValueMandatoryDescription
SECRETType: Uint8Array
Length: 1-1024 bytes
NoAsset in plaintext.
DATA_LABEL_NORMAL_1Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_2Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_3Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_4Type: Uint8Array
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_LOCAL_112+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
DATA_LABEL_NORMAL_LOCAL_212+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
DATA_LABEL_NORMAL_LOCAL_312+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
DATA_LABEL_NORMAL_LOCAL_412+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.

Constraints

The assets to be updated in batches must have the same GROUP_ID and REQUIRE_ATTR_ENCRYPTED attributes.

A maximum of 100 assets can be updated in batches.

Development Procedure

NOTE

The following is an example of using the batch update API.

Before updating an asset, ensure that the asset exists. For details about how to add an asset, see Adding an Asset. Otherwise, the NOT_FOUND error (24000002) is reported.

Update two assets in batches. Update the plaintext of the assets whose aliases are demo_alias1 and demo_alias2 to demo_pwd_new1 and demo_pwd_new2, respectively.

  1. Include the header file and define the tool function.

    import { asset } from '@kit.AssetStoreKit';
    import { util } from '@kit.ArkTS';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    function stringToArray(str: string): Uint8Array {
      let textEncoder = new util.TextEncoder();
      return textEncoder.encodeInto(str);
    }
    
  2. Develop the desired feature.

    let srcAttrs: asset.AssetMap[] = [];
    let srcAttr1: asset.AssetMap = new Map();
    srcAttr1.set(asset.Tag.ALIAS, stringToArray('demo_alias1'));
    srcAttrs.push(srcAttr1);
    let srcAttr2: asset.AssetMap = new Map();
    srcAttr2.set(asset.Tag.ALIAS, stringToArray('demo_alias2'));
    srcAttrs.push(srcAttr2);
    
    let destAttrs: asset.AssetMap[] = [];
    let destAttr1: asset.AssetMap = new Map();
    destAttr1.set(asset.Tag.SECRET, stringToArray('demo_pwd_new1'));
    destAttrs.push(destAttr1);
    let destAttr2: asset.AssetMap = new Map();
    destAttr2.set(asset.Tag.SECRET, stringToArray('demo_pwd_new2'));
    destAttrs.push(destAttr2);
    
    try {
      asset.batchUpdate(srcAttrs, destAttrs).then((res: asset.BatchResult) => {
        console.info(`Succeeded in batch updating Asset, failedCount: ${res.failedCount}`);
        if (res.failedCount > 0) {
          for (let i = 0; i < res.failedErrorInfos.length; i++) {
            console.error(`Failed to update Asset at index ${res.failedErrorInfos[i].index},
              errCode: ${res.failedErrorInfos[i].errCode}, message: ${res.failedErrorInfos[i].message}`);
          }
        }
      }).catch((err: BusinessError) => {
        console.error(`Failed to batch update Asset. Code is ${err.code}, message is ${err.message}`);
      })
    } catch (error) {
      let err = error as BusinessError;
      console.error(`Failed to batch update Asset. Code is ${err.code}, message is ${err.message}`);
    }
    

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 asset-scenario2

openharmony 鸿蒙 asset-js-group-access-control

openharmony 鸿蒙 asset-store-kit-overview

openharmony 鸿蒙 asset-js-remove

openharmony 鸿蒙 asset-js-update

openharmony 鸿蒙 asset-as-user-sys

openharmony 鸿蒙 asset-native-group-access-control

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 asset-js-batch-add

openharmony 鸿蒙 asset-native-update

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