Native AVPlayerViewController called from JavaScript causing autolayout modification from background thread
<p>I am building a TVML/TVJS Apple TV app, but i need to be able to get some native functionality with the player, so I am using <code>evaluateAppJavaScriptInContext</code> to create a JavaScript function that will push a custom view controller to the screen when called. The problem is that it causes a warning in the console:</p>

<blockquote>
  <p>This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.</p>
</blockquote>

<p>The code looks like this:</p>

<pre><code>import TVMLKit
import AVKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, TVApplicationControllerDelegate {

var window: UIWindow?

var appController: TVApplicationController?
var workoutViewController = WorkoutViewController()
static let TVBaseURL = "http://localhost:3000/"
static let TVBootURL = "\(AppDelegate.TVBaseURL)assets/tv.js"

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)

    // 1
    let appControllerContext = TVApplicationControllerContext()

    // 2
    guard let javaScriptURL = NSURL(string: AppDelegate.TVBootURL) else {
        fatalError("unable to create NSURL")
    }
    appControllerContext.javaScriptApplicationURL = javaScriptURL
    appControllerContext.launchOptions["BASEURL"] = AppDelegate.TVBaseURL

    // 3
    appController = TVApplicationController(context: appControllerContext, window: window, delegate: self)

    do {
        guard let audioURL = NSURL(string: self.workoutViewController.audioURL) else {
            fatalError("unable to create NSURL")
        }

        let audioPlayer = try AVAudioPlayer(contentsOfURL: audioURL)

        if (audioPlayer.prepareToPlay()) {
            audioPlayer.play()
        }
    } catch {
        print("Error: \(error)")
    }

    return true
}

func appController(appController: TVApplicationController, evaluateAppJavaScriptInContext jsContext: JSContext) {
    let presentWorkoutViewController : @convention(block) (String) -&gt; Void = { (string : String) -&gt; Void in

        self.workoutViewController.jsonString = string

         // dispatch_async(dispatch_get_main_queue(), {
            self.appController?.navigationController.pushViewController(self.workoutViewController, animated: true)
         // })

    }
    jsContext.setObject(unsafeBitCast(presentWorkoutViewController, AnyObject.self), forKeyedSubscript: "presentWorkoutViewController")
}
}
</code></pre>

<p>I tried to wrap it in a <code>dispatch_async</code> and that fixes the error, but when i try to push the native view controller back in view, it still contains its old content, and not the new content that i am trying to display.</p>

<p>That looked like this:</p>

<pre><code>dispatch_async(dispatch_get_main_queue(), {
    self.appController?.navigationController.pushViewController(self.workoutViewController, animated: true)
})
</code></pre>

<p>Thanks in advance!</p>