2

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!

Ben Simmons
  • 230
  • 1
  • 13
  • What do you mean by the dispatch_async? Did you tried to push a new controllers in the main thread? – Roman Podymov Sep 06 '16 at 09:58
  • @RomanPodymov: I have updated the question to include an example of what I mean. – Ben Simmons Sep 07 '16 at 13:35
  • Where is an initialisation of workoutViewController? Why don't you want to create a new instance and pass it to pushViewController? – Roman Podymov Sep 07 '16 at 13:46
  • @RomanPodymov: i am initializing a workoutViewController and i am pushing an instance to pushViewController. I have added the entire AppDelegate file for you to look at now so you can have full context. – Ben Simmons Sep 07 '16 at 14:09
  • I know that you store workoutViewController as a property for better performance but can you create it once again before push it back to view? – Roman Podymov Sep 07 '16 at 14:16
  • @RomanPodymov: That works. However, I am looping a video in the controller, and now it has a jump when the video rewinds to the beginning, where it did not before. – Ben Simmons Sep 07 '16 at 14:24
  • I think that it should be another question – Roman Podymov Sep 07 '16 at 14:27

0 Answers0