I have not been able to find an example of how to run native async code and curious with regards to iOS specifically, can something like this be done in {N}?
Objective C
:
dispatch_async(dispatch_get_main_queue(), ^{
...{N} code
});
I think there was a thread that says it cant unfortunately. But maybe im wrong
It doesn't make sense to use dispatch_async
directly (nor is it possible) because JavaScript code is always synchronized - the JavaScript block that's executed on the global queue will block the main queue for the duration of its execution, because JavaScript.
If you need to execute code on the main thread, use a promise reaction:
Promise.resolve().then(() => /* this always runs on the main thread */);
Promise reactions are guaranteed to run on the thread that owns the runtime, which is the main thread, unless you edit the platform template to run it on another thread.
If you need to call a native API asynchronously, there's the undocumented async
method:
let foo = new XYZNativeClass();
foo.methodThatTakesALongTimeToRunWithArgument.async(foo, [arg1, arg2])
.then(result => /* do stuff here */);
This works just like the regular apply
method on JavaScript functions, except it's only available on native methods and functions and will call the native API on the global queue. The promise continuation is, of course, executed back on the main thread.
@NathanWalker please note the async is iOS only. This hasn't been ported to the Android runtime; so make sure if you are doing cross platform compatible code that you put it into an ios section.
This is great insight, thank you @fealebenpae and @NathanaelA !
So ios multithreading is solved then, undocumented means in nativescript or in iOS? If in iOS , it can be removed or rejected by appstore?
@x4080: undocumented in the NativeScript runtime for iOS.
Ok cool
It will be great if the android version is available and documented :)
For those that might be interested, here's a workaround for dispatch_async: http://nuvio.us/dispatch_async
@alexziskind1 Your link is no longer working?
@hettiger - having some problems with my blog. It's back up at the moment.
@alexziskind1 saved my day. thanks :)
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.
Most helpful comment
For those that might be interested, here's a workaround for dispatch_async: http://nuvio.us/dispatch_async