Why is my interstitial Ad Producing an error?

Viewed 433

I am trying to show an interstitial Ad whenever the game over scene is displayed and I am just getting in error. I have searched for answers but they produced the same result. I am using the Notification Center method and it is not working. My banner view is working just fine though.

I am getting this error:

CoreAnimation: [EAGLContext renderbufferStorage:fromDrawable:] was called from a non-main thread in an implicit transaction! Note that this may be unsafe without an explicit CATransaction or a call to [CATransaction flush]. 2017-10-01 09:28:00.631015-0500 Split[868:273850] CoreAnimation: [EAGLContext renderbufferStorage:fromDrawable:] was called from a non-main thread in an implicit transaction! Note that this may be unsafe without an explicit CATransaction or a call to [CATransaction flush].

override func viewDidLoad() {
    super.viewDidLoad()


    NotificationCenter.default.addObserver(self, selector: #selector(GameViewController.loadAndShow), name: NSNotification.Name(rawValue: "loadAndShow"), object: nil)

    let request = GADRequest()

    request.testDevices = [kGADSimulatorID, "49d92844bdbdf74dac25e343b377744f"]

    bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)

    self.view?.addSubview(bannerView)
    bannerView.adUnitID = "ca-app-pub-9408273734198014/6590781864"

    bannerView.rootViewController = self
    bannerView.load(GADRequest())

    bannerView.delegate = self

    interstitialView = GADInterstitial(adUnitID: "ca-app-pub-9408273734198014/7472368678")

    interstitialView.load(request)

    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "StartScene") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)

        }



        view.ignoresSiblingOrder = true

        view.showsFPS = true
        view.showsNodeCount = true


    }


}


@objc func loadAndShow() {

    interstitialView = GADInterstitial()
    let request = GADRequest()
    interstitialView.setAdUnitID("ca-app-pub-9408273734198014/7472368678")
    interstitialView.delegate = self
    interstitialView.load(request)


}


func showInterstitial(ad: GADInterstitial!) {

    if (self.interstitialView.isReady) {

        interstitialView.present(fromRootViewController: self)

    }

}
1 Answers

So, as mentioned in error, you are calling interstitialView.present(fromRootViewController: self) in non main thread, it is called from a thread that was used to load the interstitial.

To fix it just present it in main queue.

DispatchQueue.main.async {
    self.interstitialView.present(fromRootViewController: self)
}
Related