6b6da774创建于 2025年12月21日历史提交

Performing Asset Operations in Specified User Space (for System Applications Only) (ArkTS)

For a singleton application that provides system-wide management functionalities, all users share an instance, which isolates data of different users. You need to specify the user space to which the operated asset belongs when:

  1. When multiple users are active at the same time, the singleton application must notify the user space to which the asset to be operated belongs so that the asset can be stored, accessed, and destroyed in the user space.
  2. When storing assets accessible after the device is first unlocked or accessible when the device is unlocked, the singleton application must specify the user space to which the assets belong.

To implement isolation and access control of the assets for singleton applications, ASSET provides a set of APIs for asset operations with the user space specified. These APIs are available only for system applications.

Constraints

The caller must have the ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS permission.

For details about how to request the permission, see Requesting Permissions for system_basic Applications.

Available APIs

The following table describes the APIs.

API Description API with User Space Not Specified
addAsUser Adds one asset in the specified user space. This API uses a promise to return the result. add
removeAsUser Removes one or more assets in the specified user space. This API uses a promise to return the result. remove
updateAsUser Updates one asset in the specified user space. This API uses a promise to return the result. update
preQueryAsUser Performs preprocessing for the asset query in the specified user space. This API is used when user authentication is required for the access to an asset. This API must be used with queryAsUser and postQueryAsUser together. This API uses a promise to return the result. preQuery
queryAsUser Queries one or more assets in the specified user space. If user authentication is required before accessing the asset, call preQueryAsUser before this API and call postQueryAsUser after this API. This API uses a promise to return the result. query if user authentication is required before the asset is accessed.
query if the asset can be accessed without user authentication.
postQueryAsUser Performs postprocessing for the asset query in the specified user space. This API is used when user authentication is required for the access to an asset. It must be used with preQueryAsUser. This API uses a promise to return the result. postQuery