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 evaluateAppJavaScriptInContext
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:
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.
The code looks like this:
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]?) -> 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) -> Void = { (string : String) -> 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")
}
}
I tried to wrap it in a dispatch_async
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.
That looked like this:
dispatch_async(dispatch_get_main_queue(), {
self.appController?.navigationController.pushViewController(self.workoutViewController, animated: true)
})
Thanks in advance!