HarmonyOS taskpool执行@Concurrent修饰的方法在访问主线程中的单例变量时,变量是空的

taskpool执行@Concurrent修饰的方法在访问主线程中的单例变量时,变量是空的,传selfHttpManage过去又报错:is not callable,但是为什么getContext()传过去可以用,代码如下:

SelfHttpManage.ets

export class SelfHttpManage { 
  private httpUtil: HttpUtil | null = null; 
  getSelfHttp(): HttpUtil { 
    if (this.httpUtil) { 
      return this.httpUtil; 
    } 
    return new HttpUtil(CommonUrlManager.getInstance().getSelfInterface()); 
  } 
} 
const selfHttpManage = new SelfHttpManage(); 
export default selfHttpManage 
public static async uploadFile(fileUri: string, categoryId: VerifyUploadCategoryId, 
    basePageConfig?: BasePageConfig): Promise<FileUploadResModel> { 
    const params: VerifyUploadParam = { 
      userId: UserInfoManager.getUserId(), 
      categoryId: categoryId, 
      streamZip: '', 
      fileName: '', // 该值会在 concurrentUploadFile 方法中自动设置 
    } 
    const uploadParam : UploadFileConfig = { 
      fileUri: fileUri, path: SelfApi.verifyUpload, params: params, basePageConfig 
    } 
    const task = new taskpool.Task(concurrentUploadFile, uploadParam, selfHttpManage, getContext()); 
    const result = await taskpool.execute(task); 
    return result as FileUploadResModel; 
      } 
@Concurrent 
export async function concurrentUploadFile(uploadParam: UploadFileConfig, httpManage: SelfHttpManage, context: Context): Promise<FileUploadResModel> { 
  const compressedImageInfo = await ImageCompressionUtil.compressImg(uploadParam.fileUri, context); 
  const formData = new FormData(); 
  formData.append('image', 'internal://cache/' + compressedImageInfo.fileName, { filename: "image.jpg", type: 'multipart/form-data' }) 
  uploadParam.params.fileName = compressedImageInfo.fileName; 
  try { 
    const result = await httpManage.getSelfHttp() 
      .uploadFile<FileUploadResModel>({ 
        url: uploadParam.path, 
        context: CommonUtil.getContext(), 
        params: uploadParam.params, 
        data: formData, 
      }); 
    try { 
      fileIo.unlinkSync(compressedImageInfo.imageUri); 
      ToastUtil.showDebugToast(`文件上传结束,删除临时文件成功:${compressedImageInfo.imageUri}`); 
    } catch (e) { 
      ToastUtil.showDebugToast(`文件上传结束,删除临时文件失败:${compressedImageInfo.imageUri}, ${JSON.stringify(e)}`); 
    } 
    return result; 
  } catch (e) { 
    try { 
      fileIo.unlinkSync(compressedImageInfo.imageUri); 
      ToastUtil.showDebugToast(`文件上传结束,删除临时文件成功:${compressedImageInfo.imageUri}`); 
    } catch (e) { 
      ToastUtil.showDebugToast(`文件上传结束,删除临时文件失败:${compressedImageInfo.imageUri}, ${JSON.stringify(e)}`); 
    } 
    throw e as Error; 
  } 
} 
export interface UploadFileConfig { 
  fileUri: string, 
  path: string, 
  params: VerifyUploadParam, 
  basePageConfig?: BasePageConfig, 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.

await httpManage.getSelfHttp()这里会报错:is not callabl。

HarmonyOS
2024-11-04 11:52:53
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

​目前的话@Concurrent函数中暂不支持从外部拿对象进行操作。因为线程模型中,TaskPool所在的线程与主线程并不共享一个ArkTS引擎实例,详细线程模型文档可参考:​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/thread-model-stage-V5

所以在执行MyStroage.getInstance()时,主线程和TaskPool线程不会拿到同一个MyStroage对象。从TaskPool的应用场景来看,也不推荐在TaskPool中使用和操作全局单例对象。​

分享
微博
QQ
微信
回复
2024-11-04 17:54:14


相关问题
HarmonyOS TaskPool线程问题
1121浏览 • 1回复 待解决
var能否修饰ArkTS变量
1161浏览 • 1回复 待解决
LocalStorageLink修饰变量会自动保存
1716浏览 • 1回复 待解决
HarmonyOS 怎么实现线程安全
1072浏览 • 1回复 待解决
hsp存在多个情况
913浏览 • 1回复 待解决
HarmonyOS taskpool参数状态变量crash
650浏览 • 1回复 待解决
zip包解压主线程还是IO线程
2350浏览 • 1回复 待解决
ArkTS声明变量public作用
2978浏览 • 1回复 待解决
Worker宿主线程必须主线程吗?
1152浏览 • 1回复 待解决