Phonegap和WebWorkers [英] Phonegap and WebWorkers

查看:72
本文介绍了Phonegap和WebWorkers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写PhoneGap/Cordova应用.

I am trying to write a PhoneGap/Cordova app.

我正在尝试在Web Workers中执行一些运行时间更长的背景知识.但是我发现Web Workers中某些功能不可用.

I am trying to do some of the more long running background stuff in Web Workers. However I am finding that some of the functionality is not available from within the Web Workers.

navigator.connection在主脚本中可用,但在Web worker中未定义,navigator.geolocation也是如此.

navigator.connection is available from within the main script but is undefined from within the web worker, same goes for navigator.geolocation.

我也想从网络工作者中访问sql-lite数据库.

I would also like to access an sql-lite database from within a web worker too.

关于在PhoneGap/Cordova中如何进行此类后台操作的任何想法?

Any ideas of how to do background operations like this from within PhoneGap/Cordova?

任何人都可以提供的帮助都会很棒.

Any help anyone can give would be great.

推荐答案

首先,您需要了解Worker是一个新线程或新进程,并且其中不包括窗口和文档对象.

First you need to understand that the Worker is a new thread or process, and this does not include the window and document objects.

Cordova在webview和本机API之间创建一个接口.如果您在worker中运行,则无法访问此API接口,因此无法使用插件或cordova核心.

Cordova creates an interface between the webview and the native API. If you run in a worker, you don't have access to this API interface, therefore you cannot use plugins or the cordova core.

我试图将cordova.js脚本导入一个工作程序:

I tried to import the cordova.js script into a worker:

loadScript('../cordova.js');

但是当找不到 window 对象时,它将引发错误.最后,模拟对象:

But it throws an error when it does not find the window object. Finally, emulating the objects:

self.window = this;
self.window.document = this;
loadScript('../cordova.js');

cordova的脚本抛出"ReferenceError:提示未定义".

The cordova's script throws "ReferenceError: promp is not defined".

另一方面,您需要了解WebView和本机代码之间的通信是异步的.例如,如果您发送一个SQLite查询,则您的JavaScript代码正在继续运行,当查询解决后,API会将事件发送到WebView,然后您运行回调.

On the other hand, you need to understand to, the communication between the WebView and the native code, are asynchronous. If you send a SQLite query for example, your JavaScript code are continuing runs, when the query is resolved, the API sends an event to the WebView and you runs your callback.

例如,我使用工作程序来加密数据,因为此过程太困难并且会导致阻塞.但是,如果您需要使用cordova插件,就不会有此问题.

I use workers for example to encrypt data, because this process is too hard and causes blocking. But if you need to use cordova plugins, you won't have this problem.

有一个解释了解这一点.

对于SQLite,我建议您使用 Cordova-SQLitePlugin .

For SQLite, I recommend you use Cordova-SQLitePlugin.

如果您需要自己的高级程序,则可以了解如何制作插件: https://cordova.apache.org/docs/en/4.0.0/guide_hybrid_plugins_index.md.html

If you need your own hight-process, You can learn about how to make plugins: https://cordova.apache.org/docs/en/4.0.0/guide_hybrid_plugins_index.md.html

在此期间,您可以使用worker并发送和接收数据,但不能使用资源引用.并请注意,使用api(如SQLite)将是异步的,您无需打开其他进程即可执行它们.您可以将结果发送给工作人员,然后从那里进行工作.

In the meantime, you can use workers and send and received data, but not with resources references. And note that using apis (like SQLite), this will be asynchronous and you don't need to open another process to perform them. You can just send the result to a worker and work it from there.

这篇关于Phonegap和WebWorkers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆