工人和主线程的共享本机实例

时间:2019-02-19 20:25:33

标签: nativescript

我要下载一个Progressbar文件。已加载文件的计算需要在单独的线程中完成。只是不清楚如何传递变量manager

main.js

import * as MyWorker from 'nativescript-worker-loader!~/utils/DownloadManagerProgress.js';

const { DOWNLOAD_SERVICE } = android.content.Context;
const manager = app.android.context.getSystemService(DOWNLOAD_SERVICE);

const worker = new MyWorker();
worker.postMessage({ manager });

worker.onmessage = function (event) {
  console.log(event);
};

worker.onerror = function (err) {
  console.log(`An unhandled error occurred in worker: ${err.filename}, line: 
  ${err.lineno} :`);
  console.log(err.message);
};

DownloadManagerProgress.js

import 'globals';


global.onmessage = (msg) => {
  const { manager } = msg.data;
  const query = new android.app.DownloadManager.Query();
  const cursor = manager.query(query);
};

错误:

  

“未捕获的TypeError:无法读取未定义的属性\'query \'”


如果我在worker中创建对象,也会出现错误

DownloadManagerProgress.js

import 'globals';
import * as app from 'application';

global.onmessage = (msg) => {
  const { DOWNLOAD_SERVICE } = android.content.Context;
  const manager = app.android.context.getSystemService(DOWNLOAD_SERVICE);

  const query = new android.app.DownloadManager.Query();
  const cursor = manager.query(query);
};

错误:

  

未捕获的TypeError:无法读取未定义的属性\'getSystemService \'

请帮我。谢谢。

0 个答案:

没有答案