目录

RCP网络请求在TaskPool子线程中使用异常

问题现象

RCP网络请求库结合TaskPool使用,无法发请求,日志打印undefined is not callable,关键代码示例:

@Concurrent
function downloadToFile(savePath: string, session: rcp.Session) {
  session.downloadToFile(downloadUrl, downloadToFile).then((response) => {
    console.info(`Succeeded in getting the response ${response}`);
  }).catch((err: BusinessError) => {
    console.error(`DownloadToFile failed, the error message is ${JSON.stringify(err)}`);
  });
}

@Entry
@Component
struct SendReqInTaskPool {
  context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  savePath = this.context.filesDir;
  session = rcp.createSession();

  build() {
    Column() {
      Button('在taskPool内发rcp请求').onClick(() => {
        taskpool.execute(downloadToFile, this.savePath, this.session);
      });
    }
    .height('100%')
    .width('100%');
  }
}

背景知识

问题定位

session实例中包含原型及方法,不属于普通对象。

分析结论

rcp.createSession创建的实例对象不支持在taskpool.execute传递。

修改建议

将session实例的创建放在@Concurrent装饰的函数内即可,修改之后的代码参考如下:

import { taskpool } from '@kit.ArkTS';
import { rcp } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';

@Concurrent
function downloadToFile(savePath: string) {
  let downloadUrl: string = 'xx.xx.xx'; // 开发者自行配置
  let downloadToFile: rcp.DownloadToFile = {
    kind: 'folder',
    path: savePath // 请根据自身业务选择合适的路径
  } as rcp.DownloadToFile;
  const session = rcp.createSession();
  session.downloadToFile(downloadUrl, downloadToFile).then((response) => {
    console.info(`Succeeded in getting the response ${response}`);
  }).catch((err: BusinessError) => {
    console.error(`DownloadToFile failed, the error message is ${JSON.stringify(err)}`);
  });
}

@Entry
@Component
struct SendReqInTaskPool {
  context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  savePath = this.context.filesDir;

  build() {
    Column() {
      Button('在taskPool内发rcp请求').onClick(() => {
        taskpool.execute(downloadToFile, this.savePath);
      });
    }
    .height('100%')
    .width('100%');
  }
}

总结

异步处理操作主要有Promise、Work线程、TaskPool任务池三种方式。HarmonyOS应用中每个进程都会有一个主线程,除主线程外,还有一类与主线程并行的独立线程Worker,主要用于执行耗时操作,最多可以创建64个Worker。

根据性能要求进行选择,如果仅使用async\Promise等类似协程的方法,耗时操作还是会阻塞主线程UI更新,因为它们是在同一个线程中执行的,同一个线程中就不存在多任务同时执行这样的情况。async\Promise实际上是单线程任务执行顺序排列,执行到耗时操作时(如文件IO、网络IO等),在执行完之前是执行不了其它任务,所以会阻塞UI刷新。解决办法是通过TaskPool或者Worker去执行耗时操作,执行完后再把数据返回给UI线程。TaskPool和Worker差异参考:TaskPool和Worker的对比。应用多线程有数量限制,网络请求这块推荐使用Promise.all来进行处理。