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, 
}

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

HarmonyOS
4天前
浏览
收藏 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
微信
回复
4天前
相关问题
var能否修饰ArkTS变量
364浏览 • 1回复 待解决
LocalStorageLink修饰变量会自动保存
863浏览 • 1回复 待解决
hsp存在多个情况
113浏览 • 1回复 待解决
zip包解压主线程还是IO线程
1528浏览 • 1回复 待解决
Worker宿主线程必须主线程吗?
291浏览 • 1回复 待解决
ArkTS声明变量public作用
2165浏览 • 1回复 待解决
TaskPool线程主线程如何通信
2193浏览 • 1回复 待解决
@State 修饰变量值改变,界面不刷新
1203浏览 • 1回复 待解决
HarmonyOS Entry和Hsp创建失效
305浏览 • 1回复 待解决
TaskPool如何跟主线程进行通信
399浏览 • 1回复 待解决