HarmonyOS context在worker中传递报错?

代码如下

@Sendable
export default class InsertMsgDbModel {
  public tableName: string = '';
  public dbName: string = '';
  public userId: string = GlobalConf.channel;
  public sendableContext: sendableContextManager.SendableContext;
  constructor(sendableContext: sendableContextManager.SendableContext) {
    this.sendableContext = sendableContext;
  }
}

let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context);

const insertDbModel = new InsertMsgDbModel(sendableContext);
private workerStage1: worker.ThreadWorker = new worker.ThreadWorker('ZhBaseLib/ets/workers/ZHBaseWorker.ets');

this.workerStage1.postMessage(insertDbModel);

删除sendableContext 后可以正常传递,加上后提示序列化失败。按官方文档应该是支持的。

阅读 427
1 个回答

使用sendableContext需要配合使用postMessageWithSharedSendable方法,这边看您使用的是postMessage,可尝试替换为postMessageWithSharedSendablesendableContextManager.convertFromContext的示例中使用的也是postMessageWithSharedSendable,请替换使用

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-worker-V5\#postmessagewithsharedsendable12

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-app-ability-sendablecontextmanager-V5\#sendablecontextmanagerconvertfromcontext

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进