In our tvOS app we have a couple of hooks into the native side where we receive events over a WebSocket in (using CocoaAsyncSocket) native and propagate those events into the JavaScript layer. The calls into the JS layer are structured like this:
self.receiveQueue.addOperation {
websocket.invokeMethod("onmessage", withArguments: [["data": text]])
}
Where `receiveQueue` is an `OperationQueue`. This works great most of the time, but we occasionally see application crashes where it seems a memory reference has gone bad. However, we've found that if we take the JavaScript function being called and wrap it in a
setTimeout(() => {
// Do the work here
}, 0)
this resolves nearly all the issues. We still get an application crash here and there but the `setTimeout` does the trick most of the time.
This leads me to the following question. What's the proper way to call back to a TVJS application from native? Should I be using DispatchQueue.main.async { } instead of an OperationQueue? How does this relate to doing work on the main UI thread?
Thanks!