harmony 鸿蒙@ohos.app.ability.sendableContextManager (sendableContextManager)
@ohos.app.ability.sendableContextManager (sendableContextManager)
The sendableContextManager module provides APIs for converting between Context and SendableContext objects.
NOTE
- The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.
- The APIs of this module can be used only in the stage model.
When to Use
This module is used to transfer data between concurrent ArkTS instances (including the main thread and the worker thread of TaskPool or Worker).
For example, when transferring sendable data from the main thread to a child thread, the following conversion steps are involved to ensure efficient data transfer: - Conversion from Context to SendableContext for the main thread to transfer sendable data to the child thread. - Conversion from SendableContext to Context for the child thread to use the sendable data.
The Context here is different from that created by createModuleContext. The differences are as follows: - Context involved in the conversion: ArkTS concurrent instances hold different application-side Context instances that correspond to the same underlying Context object. When the Context properties and methods in an instance are modified, the Context properties and methods in the related instances are modified accordingly. The eventHub attribute in the Context instance is special. The eventHub objects in different instances are independent from each other and cannot be used across ArkTS instances. If you want to use EventHub to transfer data across instances, call setEventHubMultithreadingEnabled to enable the cross-thread data transfer feature.
- Context created using createModuleContext: ArkTS concurrent instances hold different application-side Context objects that correspond to different underlying Context objects.
Constraints
The Context types used in the conversion must be the same. Currently, the following types of Context support conversion: Context, ApplicationContext, AbilityStageContext, and UIAbilityContext.
Modules to Import
import { sendableContextManager } from '@kit.AbilityKit';
Properties
System capability: SystemCapability.Ability.AbilityRuntime.Core
Atomic service API: This API can be used in atomic services since API version 12.
Name | Type | Mandatory | Description |
---|---|---|---|
SendableContext | SendableContext | Yes | Level-2 module SendableContext. |
Example
import { sendableContextManager } from '@kit.AbilityKit';
let sendableContext: sendableContextManager.SendableContext;
sendableContextManager.convertFromContext
convertFromContext(context: common.Context): SendableContext
Converts a Context object to a SendableContext object.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Atomic service API: This API can be used in atomic services since API version 12.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
context | common.Context | Yes | Context object. The Context base class, and its child classes ApplicationContext, AbilityStageContext, and UIAbilityContext are supported. |
Return value
Type | Description |
---|---|
SendableContext | SendableContext object. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
Example
import { AbilityConstant, UIAbility, Want, sendableContextManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { worker } from '@kit.ArkTS';
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext) {
this.sendableContext = sendableContext;
}
sendableContext: sendableContextManager.SendableContext;
// other sendable object
}
export default class EntryAbility extends UIAbility {
worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets');
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
// convert and post
try {
let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context);
let object: SendableObject = new SendableObject(sendableContext);
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message');
this.worker.postMessageWithSharedSendable(object);
} catch (error) {
hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error));
}
}
}
sendableContextManager.convertToContext
convertToContext(sendableContext: SendableContext): common.Context
Converts a SendableContext object to a Context object.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Atomic service API: This API can be used in atomic services since API version 12.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
sendableContext | SendableContext | Yes | SendableContext object. |
Return value
Type | Description |
---|---|
common.Context | Context object. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
Example
Context passed by the main thread:
import { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { worker } from '@kit.ArkTS';
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) {
this.sendableContext = sendableContext;
this.contextName = contextName;
}
sendableContext: sendableContextManager.SendableContext;
contextName: string;
}
export default class EntryAbility extends UIAbility {
worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets');
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
// convert and post
try {
let context: common.Context = this.context as common.Context;
let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(context);
let object: SendableObject = new SendableObject(sendableContext, 'BaseContext');
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message');
this.worker.postMessageWithSharedSendable(object);
} catch (error) {
hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error));
}
}
}
Context received by the Worker thread:
import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
import { common, sendableContextManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) {
this.sendableContext = sendableContext;
this.contextName = contextName;
}
sendableContext: sendableContextManager.SendableContext;
contextName: string;
}
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
workerPort.onmessage = (e: MessageEvents) => {
let object: SendableObject = e.data;
let sendableContext: sendableContextManager.SendableContext = object.sendableContext;
if (object.contextName == 'BaseContext') {
hilog.info(0x0000, 'testTag', '%{public}s', 'convert to context.');
try {
let context: common.Context = sendableContextManager.convertToContext(sendableContext);
// Obtain the sandbox path after obtaining the Context object.
hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir);
} catch (error) {
hilog.error(0x0000, 'testTag', 'convertToContext failed %{public}s', JSON.stringify(error));
}
}
}
workerPort.onmessageerror = (e: MessageEvents) => {
hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror');
}
workerPort.onerror = (e: ErrorEvent) => {
hilog.info(0x0000, 'testTag', '%{public}s', 'onerror');
}
sendableContextManager.convertToApplicationContext
convertToApplicationContext(sendableContext: SendableContext): common.ApplicationContext
Converts a SendableContext object to an ApplicationContext object.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Atomic service API: This API can be used in atomic services since API version 12.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
sendableContext | SendableContext | Yes | SendableContext object. |
Return value
Type | Description |
---|---|
common.ApplicationContext | ApplicationContext object. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
Example
Context passed by the main thread:
import { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { worker } from '@kit.ArkTS';
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) {
this.sendableContext = sendableContext;
this.contextName = contextName;
}
sendableContext: sendableContextManager.SendableContext;
contextName: string;
}
export default class EntryAbility extends UIAbility {
worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets');
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
// convert and post
try {
let context: common.Context = this.context as common.Context;
let applicationContext = context.getApplicationContext();
let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(applicationContext);
let object: SendableObject = new SendableObject(sendableContext, 'ApplicationContext');
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message');
this.worker.postMessageWithSharedSendable(object);
} catch (error) {
hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error));
}
}
}
Context received by the Worker thread:
import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
import { common, sendableContextManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) {
this.sendableContext = sendableContext;
this.contextName = contextName;
}
sendableContext: sendableContextManager.SendableContext;
contextName: string;
}
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
workerPort.onmessage = (e: MessageEvents) => {
let object: SendableObject = e.data;
let sendableContext: sendableContextManager.SendableContext = object.sendableContext;
if (object.contextName == 'ApplicationContext') {
hilog.info(0x0000, 'testTag', '%{public}s', 'convert to application context.');
try {
let context: common.ApplicationContext = sendableContextManager.convertToApplicationContext(sendableContext);
// Obtain the sandbox path after obtaining the Context object.
hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir);
} catch (error) {
hilog.error(0x0000, 'testTag', 'convertToApplicationContext failed %{public}s', JSON.stringify(error));
}
}
}
workerPort.onmessageerror = (e: MessageEvents) => {
hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror');
}
workerPort.onerror = (e: ErrorEvent) => {
hilog.info(0x0000, 'testTag', '%{public}s', 'onerror');
}
sendableContextManager.convertToAbilityStageContext
convertToAbilityStageContext(sendableContext: SendableContext): common.AbilityStageContext
Converts a SendableContext object to an AbilityStageContext object.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Atomic service API: This API can be used in atomic services since API version 12.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
sendableContext | SendableContext | Yes | SendableContext object. |
Return value
Type | Description |
---|---|
common.AbilityStageContext | AbilityStageContext object. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
Example
Context passed by the main thread:
import { UIAbility, sendableContextManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { worker } from '@kit.ArkTS';
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) {
this.sendableContext = sendableContext;
this.contextName = contextName;
}
sendableContext: sendableContextManager.SendableContext;
contextName: string;
}
export default class EntryAbility extends UIAbility {
worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets');
onCreate(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage onCreate');
// convert and post
try {
let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context);
let object: SendableObject = new SendableObject(sendableContext, 'AbilityStageContext');
hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage post message');
this.worker.postMessageWithSharedSendable(object);
} catch (error) {
hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error));
}
}
}
Context received by the Worker thread:
import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
import { common, sendableContextManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) {
this.sendableContext = sendableContext;
this.contextName = contextName;
}
sendableContext: sendableContextManager.SendableContext;
contextName: string;
}
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
workerPort.onmessage = (e: MessageEvents) => {
let object: SendableObject = e.data;
let sendableContext: sendableContextManager.SendableContext = object.sendableContext;
if (object.contextName == 'AbilityStageContext') {
hilog.info(0x0000, 'testTag', '%{public}s', 'convert to abilitystage context.');
try {
let context: common.AbilityStageContext = sendableContextManager.convertToAbilityStageContext(sendableContext);
// Obtain the sandbox path after obtaining the Context object.
hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir);
} catch (error) {
hilog.error(0x0000, 'testTag', 'convertToAbilityStageContext failed %{public}s', JSON.stringify(error));
}
}
}
workerPort.onmessageerror = (e: MessageEvents) => {
hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror');
}
workerPort.onerror = (e: ErrorEvent) => {
hilog.info(0x0000, 'testTag', '%{public}s', 'onerror');
}
sendableContextManager.convertToUIAbilityContext
convertToUIAbilityContext(sendableContext: SendableContext): common.UIAbilityContext
Converts a SendableContext object to a UIAbilityContext object.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Atomic service API: This API can be used in atomic services since API version 12.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
sendableContext | SendableContext | Yes | SendableContext object. |
Return value
Type | Description |
---|---|
common.UIAbilityContext | UIAbilityContext object. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
Example
Context passed by the main thread:
import { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { worker } from '@kit.ArkTS';
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) {
this.sendableContext = sendableContext;
this.contextName = contextName;
}
sendableContext: sendableContextManager.SendableContext;
contextName: string;
}
export default class EntryAbility extends UIAbility {
worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets');
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
// convert and post
try {
let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context);
let object: SendableObject = new SendableObject(sendableContext, 'EntryAbilityContext');
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message');
this.worker.postMessageWithSharedSendable(object);
} catch (error) {
hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error));
}
}
}
Context received by the Worker thread:
import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
import { common, sendableContextManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) {
this.sendableContext = sendableContext;
this.contextName = contextName;
}
sendableContext: sendableContextManager.SendableContext;
contextName: string;
}
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
workerPort.onmessage = (e: MessageEvents) => {
let object: SendableObject = e.data;
let sendableContext: sendableContextManager.SendableContext = object.sendableContext;
if (object.contextName == 'EntryAbilityContext') {
hilog.info(0x0000, 'testTag', '%{public}s', 'convert to uiability context.');
try {
let context: common.UIAbilityContext = sendableContextManager.convertToUIAbilityContext(sendableContext);
// Obtain the sandbox path after obtaining the Context object.
hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir);
} catch (error) {
hilog.error(0x0000, 'testTag', 'convertToUIAbilityContext failed %{public}s', JSON.stringify(error));
}
}
}
workerPort.onmessageerror = (e: MessageEvents) => {
hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror');
}
workerPort.onerror = (e: ErrorEvent) => {
hilog.info(0x0000, 'testTag', '%{public}s', 'onerror');
}
sendableContextManager.setEventHubMultithreadingEnabled20+
setEventHubMultithreadingEnabled(context: common.Context, enabled: boolean): void
Enables the cross-thread data transfer feature of EventHub in a Context object.
NOTE
- When multiple Context objects communicate, you need to call this API to set each Context object to support EventHub cross-thread data transfer.
- Before this API is called, data is passed by reference. After this API is called, data is passed through serialization, which means that the data of the sender thread is independent of that of the receiver thread.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Atomic service API: This API can be used in atomic services since API version 20.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
context | common.Context | Yes | Context object. For details about the serialization data types supported by Eventhub, see Sequenceable Data Types. The data size cannot exceed 16 MB. |
enabled | boolean | Yes | Whether to enable the cross-thread data transfer feature. The value true means to enable the feature, and false means the opposite. |
Example
Enable the cross-thread data transfer feature of EventHub in a Context object in the main thread, convert the Context object to a SenableContext object, and send the SendableContext object to the Worker thread.
import { common, sendableContextManager } from '@kit.AbilityKit';
import { worker } from '@kit.ArkTS';
import { hilog } from '@kit.PerformanceAnalysisKit';
const DOMAIN = 0x0000;
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) {
this.sendableContext = sendableContext;
this.contextName = contextName;
}
sendableContext: sendableContextManager.SendableContext;
contextName: string;
}
@Entry
@Component
struct Index {
@State context: common.Context|undefined = this.getUIContext().getHostContext();
worker1: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets');
aboutToAppear(): void {
let context: common.Context = this.context as common.Context;
context.eventHub.on('event1', this.eventFunc);
context.eventHub.emit('event1', 'xingming', 22);
}
eventFunc(name: string, age: number) {
hilog.info(DOMAIN, 'testTag', 'name %{public}s age %{public}d', name, age);
}
build() {
Column() {
Row() {
Button('thread 1')
.size({ width: 100, height: 100 })
.onClick(() => {
if (this.context == undefined) {
return;
}
sendableContextManager.setEventHubMultithreadingEnabled(this.context, true);
let sendableContext: sendableContextManager.SendableContext =
sendableContextManager.convertFromContext(this.context);
let object: SendableObject = new SendableObject(sendableContext, 'BaseContext');
this.worker1.postMessageWithSharedSendable(object);
})
}
}
}
}
After receiving the SendableContext object in the Worker thread, convert it to a Context object. Then, enable the cross-thread data transfer feature of EventHub in the Context object in the Worker thread, and send a message back to the main thread using this feature.
import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
import { common, sendableContextManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
const DOMAIN = 0x0000;
@Sendable
export class SendableObject {
constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) {
this.sendableContext = sendableContext;
this.contextName = contextName;
}
sendableContext: sendableContextManager.SendableContext;
contextName: string;
}
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
workerPort.onmessage = (e: MessageEvents) => {
let object: SendableObject = e.data;
let sendableContext: sendableContextManager.SendableContext = object.sendableContext;
if (object.contextName == 'BaseContext') {
try {
let context: common.Context = sendableContextManager.convertToContext(sendableContext);
sendableContextManager.setEventHubMultithreadingEnabled(context, true);
context.eventHub.emit('event1', 'xingming', 40);
} catch (error) {
hilog.error(DOMAIN, 'testTag', 'convertToContext failed %{public}s', JSON.stringify(error));
}
}
};
workerPort.onmessageerror = (e: MessageEvents) => {
hilog.error(DOMAIN, 'testTag', '%{public}s', 'onmessageerror');
};
workerPort.onerror = (e: ErrorEvent) => {
hilog.error(DOMAIN, 'testTag', '%{public}s', 'onerror');
};
你可能感兴趣的鸿蒙文章
harmony 鸿蒙AbilityAccessControl
harmony 鸿蒙OH_NativeBundle_ApplicationInfo
harmony 鸿蒙OH_NativeBundle_ElementName
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦