Nativescript: Question on multiple Workers

Created on 29 Nov 2016  路  5Comments  路  Source: NativeScript/NativeScript

If I initialize a Worker like var worker = new Worker("./path/to/worker") it will create a new instance of UIDocumentInteractionControllerDelegateImpl with suffix ...Impl1. And if I create a new worker once again (under the same variable), the suffix becomes ...Impl2. Sounds logical, since I never did a worker.terminate() or close(). However, if I do terminate the 1st instance, the 2nd one will still be called "...Impl2". The warning messages I get are as such:

CONSOLE WARN file:///app/tns_modules/utils/utils.js:175:14: Objective-C class name "UIDocumentInteractionControllerDelegateImpl" is already in use - using "UIDocumentInteractionControllerDelegateImpl1" instead.

CONSOLE WARN file:///app/tns_modules/utils/utils.js:175:14: Objective-C class name "UIDocumentInteractionControllerDelegateImpl" is already in use - using "UIDocumentInteractionControllerDelegateImpl2" instead.

Are they actual warnings that I can prevent from happening or am I doing something wrong? All I'm doing is pretty much this (inside a Button tap):

var msg = { foo: "bar" };
var worker = new Worker("./worker");
worker.postMessage(msg);
worker.onmessage = function(m) {
  // Do something with the message...
  worker.terminate();
};
ios question

Most helpful comment

@NordlingArt Creating a new worker instance initializes a brand new JavaScript virtual machine which comes with a price. Whether to terminate the worker and recreate it every time or reuse a single worker depends on the particular case, but in general it is better to reuse an already created worker instead of creating a new one. A not terminated worker can occupy some memory and you have to decide if this is an issue in your particular case.
However, if you choose to terminate and recreate the worker instance, still you should not be scared by these warnings (I described above what causes the warning messages).

Besides these warnings are there any errors or unexpected behavour?

All 5 comments

Hi @NordlingArt
I guess that you are creating a class in your worker that inherits from UIDocumentInteractionControllerDelegate. If you don't specify explicitly the name of the newly created class, like this:

var MyClass = UIViewController.extend({ ... }, { name: "MyCustomName" });

the iOS runtime will generate one for you (in your case UIDocumentInteractionControllerDelegateImpl1). The next time you create a worker instance, the worker script will be executed again and a brand new class will be created and registered in the Objective-C runtime. To prevent a name clash, the iOS runtime will generate unique name like UIDocumentInteractionControllerDelegateImpl2 and so on. After worker termination the newly created UIDocumentInteractionControllerDelegate successor will not be unregistered. It still exists as a registered native class in the Objective-C runtime, but if some of its methods are implemented in JavaScript, they can be called only in the context of the VM in which the class is created.

You are doing nothing wrong, but if the warning message irritates you, pass a unique name for every successor when creating it.

Also, I will recommend you to create only one worker instance and reuse it, instead of terminating one and after a while creating a new one.

@ivanbuhov - I am never extending a native class such as UIViewController. Only creating the worker instance(s) inside a regular function call. Inside the worker I am processing CGImage/CIImage/CIFilter.

As for your recommendation. Do you mean that I should never terminate the worker instance then? Because let's say I have three pages, whereas only one of them makes use of this instance. After this certain page has been called, the worker will thereby exist forever unless I terminate it. Are there any performance issues to NEVER terminate the a worker?

@NordlingArt Creating a new worker instance initializes a brand new JavaScript virtual machine which comes with a price. Whether to terminate the worker and recreate it every time or reuse a single worker depends on the particular case, but in general it is better to reuse an already created worker instead of creating a new one. A not terminated worker can occupy some memory and you have to decide if this is an issue in your particular case.
However, if you choose to terminate and recreate the worker instance, still you should not be scared by these warnings (I described above what causes the warning messages).

Besides these warnings are there any errors or unexpected behavour?

@ivanbuhov thanks for shining some light there i was wondering 馃 if i should use multiple workers or reuse one for the entire app atm i got like 5 馃槅 for a couple pages

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

valentinstoychev picture valentinstoychev  路  3Comments

NickIliev picture NickIliev  路  3Comments

dhanalakshmitawwa picture dhanalakshmitawwa  路  3Comments

yclau picture yclau  路  3Comments

tsonevn picture tsonevn  路  3Comments