目录

Creating a Sendable Strong Reference to an ArkTS Object Using Node-API Extension APIs

HarmonyOS APIs provide the capability of sharing strong references across ArkTS threads in the same process. Unlike napi_ref, napi_sendable_ref supports operations across ArkTS threads, but also has some restrictions.

When to Use

You can use the napi_create_strong_sendable_reference API to create a Sendable strong reference to a Sendable ArkTS object, use the napi_get_strong_sendable_reference_value API to obtain the referenced ArkTS object, and use the napi_delete_strong_sendable_reference API to delete the Sendable strong reference. These operations can be performed in the same ArkTS thread or different ArkTS threads.

Available APIs

API Description
napi_create_strong_sendable_reference Creates a Sendable strong reference to a Sendable ArkTS object.
napi_delete_strong_sendable_reference Deletes a Sendable strong reference.
napi_get_strong_sendable_reference_value Obtains the ArkTS object value associated with a Sendable strong reference.

Sample Code

Precautions

  1. napi_sendable_ref can be created only for Sendable objects.
  2. napi_sendable_ref can be used across ArkTS threads. When performing multithreaded operations, the caller must manage the release timing to avoid issues related to using after release.
  3. Within the same process, a maximum of 51200 napi_sendable_ref instances can coexist.
  4. napi_sendable_ref is different from other reference types. Therefore, you cannot forcibly convert napi_ref or napi_strong_ref to napi_sendable_ref. napi_delete_strong_sendable_reference and napi_get_strong_sendable_reference_value accept only napi_sendable_ref created by calling napi_create_strong_sendable_reference.
  5. When using the napi_create_strong_sendable_reference, napi_get_strong_sendable_reference_value, and napi_delete_strong_sendable_reference APIs, ensure that the input env parameter is the ArkTS thread environment object of the current API. Otherwise, multithreading safety issues may occur.