12

I am trying to write a PhoneGap/Cordova app.

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 is available from within the main script but is undefined from within the web worker, same goes for navigator.geolocation.

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

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

Any help anyone can give would be great.

Neaox
  • 1,933
  • 3
  • 18
  • 29

3 Answers3

19

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 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.

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

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

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');

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

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.

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.

There is an explanation to understand this.

For SQLite, I recommend you use Cordova-SQLitePlugin.

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

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Exos
  • 3,958
  • 2
  • 22
  • 30
  • Thank you for a well thought out answer with references. Your english isn't too bad..especially considering we are using complex terms for a specific topic. The bounty is yours! – Neaox Mar 02 '15 at 03:54
1

I would imaging that you could pass those to the worker with message. Something like suggested in here:

javascript web workers - how do I pass arguments?

As for the sql-lite db you should be able to initialize a connection lib from within a worker script much the same way you would your main script.

I realize this answer may not be bounty worthy but may get you started in the right direction

Community
  • 1
  • 1
Chase
  • 9,289
  • 5
  • 51
  • 77
  • Hi, thanks for the suggestion, this won't work for the purpose I need as this will pass the values in their current state. If passed to the WebWorker the navigator.connection will not change within the worker when it changes in the main thread. I haven't tried it with the SQLlite object but I suspect there will be issues with passing this in too, I will give it a go and get back to you. Thanks again for the suggestion. – Neaox Feb 23 '15 at 06:13
  • @Neaox "If passed to the WebWorker the navigator.connection will not change within the worker when it changes in the main thread" This is true: there aren't any shared objects AFAIK between the worker and the main thread. Everything has to be communicated by explicit messages. – Michal Charemza Feb 24 '15 at 17:43
1

Due to the fact that your Web Workers run outside of the main application thread they do not have the same access to JavaScript features as your main application does. Your workers do not have access to:

  • The DOM
  • The document object
  • The window object
  • The parent object

If you want your application in the UI thread to communicate with a worker you need to pass the object through the message. But since worker accepts string, you can use JSON.parse() or JSON.stringify() to successfully send the object.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62