目录

Updating an Asset (ArkTS)

Available APIs

You can use update(query: AssetMap, attributesToUpdate: AssetMap) (asynchronous API) or updateSync(query: AssetMap, attributesToUpdate: AssetMap) (synchronous API) to update an asset.

The following table describes the attributes of AssetMap for updating an asset.

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

Example

This module provides asynchronous and synchronous APIs. The following uses the asynchronous APIs as an example. For more information about the APIs, see @ohos.security.asset (Asset Store Service).

For details about how to update an asset in a group, see Updating an asset in a Group.

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 asset demo_alias as follows: change the asset plaintext to demo_pwd_new and additional attribute to demo_label_new.

  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 query: asset.AssetMap = new Map();
    query.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
    let attrsToUpdate: asset.AssetMap = new Map();
    attrsToUpdate.set(asset.Tag.SECRET, stringToArray('demo_pwd_new'));
    attrsToUpdate.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label_new'));
    try {
      asset.update(query, attrsToUpdate).then(() => {
        console.info(`Succeeded in updating Asset.`);
        // ...
      }).catch((err: BusinessError) => {
        console.error(`Failed to update Asset. Code is ${err.code}, message is ${err.message}`);
        // ...
      });
    } catch (error) {
      let err = error as BusinessError;
      console.error(`Failed to update Asset. Code is ${err.code}, message is ${err.message}`);
      // ...
    }