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
赞
收藏 0
回答 1
待解决
相关问题
HarmonyOS taskpool中使用单例与主线程创建的单例不一致
574浏览 • 1回复 待解决
HarmonyOS taskPool执行的task内如何回调主线程的方法
1322浏览 • 1回复 待解决
HarmonyOS TaskPool子线程单例问题
833浏览 • 1回复 待解决
var能否修饰ArkTS中的变量?
905浏览 • 1回复 待解决
HarmonyOS 使用taskpool过程中,对于@Concurrent修饰的方法初始化失败
483浏览 • 1回复 待解决
HarmonyOS @Concurrent修饰的方法中引入对象报错
565浏览 • 1回复 待解决
TaskPool在任务执行过程中如何跟主线程进行通信?如何操作同一块内存变量
2562浏览 • 1回复 待解决
HarmonyOS @Sendable中的静态变量,如果在一个线程中创建了类的静态变量,能否在另一个线程中访问这个静态变量
496浏览 • 1回复 待解决
LocalStorageLink修饰的变量会自动保存
1443浏览 • 1回复 待解决
HarmonyOS 为什么static修饰的变量,结束应用、重启变量的值还存在
473浏览 • 1回复 待解决
HarmonyOS 单例模式下在主线程和用taskpool创建的子线程内通过getInstance获取到的对象不是同一个对象
329浏览 • 1回复 待解决
HarmonyOS $$值传递实现原理与build方法重新渲染的是状态修饰的变量还是全局渲染
624浏览 • 1回复 待解决
类似CallStateObserver中的回调,是在app主线程,还新的线程中?
5678浏览 • 1回复 待解决
HarmonyOS 怎么实现线程安全的单例类
735浏览 • 1回复 待解决
单例在hsp中存在多个的情况
705浏览 • 1回复 待解决
事件系统eventHub,在组件中使用 eventHub.on 订阅的方法,不能访问组件的变量
1513浏览 • 1回复 待解决
zip包的解压是在主线程还是IO线程
2047浏览 • 1回复 待解决
ArkTS的单例模式下判空逻辑会执行两次是什么回事
1011浏览 • 1回复 待解决
Worker的宿主线程必须是主线程吗?
903浏览 • 1回复 待解决
HarmonyOS taskpool参数是状态变量crash
373浏览 • 1回复 待解决
ArkTS中声明变量时public的作用
2690浏览 • 1回复 待解决
HarmonyOS 使用taskpool执行task后,返回的结果没有通过Promise返回主线程,有什么定位方法吗?
614浏览 • 1回复 待解决
HarmonyOS 单例模式打开已有page,page执行什么方法
313浏览 • 1回复 待解决
目前的话@Concurrent函数中暂不支持从外部拿对象进行操作。因为线程模型中,TaskPool所在的线程与主线程并不共享一个ArkTS引擎实例,详细线程模型文档可参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/thread-model-stage-V5
所以在执行MyStroage.getInstance()时,主线程和TaskPool线程不会拿到同一个MyStroage对象。从TaskPool的应用场景来看,也不推荐在TaskPool中使用和操作全局单例对象。