0

I'm building a simple game that has only 3 UIViewControllers, a home screen, the GameViewController, and a pause modal. When I launch the app from Xcode on my iPhone 6s, the CPU usage starts at 0%. When I segue to the GameViewController, the CPU usage will spike up, but when I segue back to the home screen, it will no longer be at 0% but will be above 10%. If I continue to segue back and forth, the CPU usage on the home screen will increase by about 10% each time. Here's the code I've been using to segue:

func pageSegue(_ currentScreen: String) {

    let newVC: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: currentScreen) as UIViewController
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = newVC
}

The above code finds the Storyboard identifier (String) of view controller that it's going to segue to and then calls the segue.

The screenshot below is what Instruments looks like after segueing to the GameViewController and then back to the home screen only once. The percentages in the highlighted row and those above it continue to climb after doing this.

I've also tried to add the following code to viewDidDisappear with no luck:

//gameScene is the SKView
gameScene.scene?.isPaused = true
for child in (gameScene.scene?.children)! {
    child.removeChildren(in: [child])
}
gameScene.presentScene(nil)
gameScene.removeFromSuperview()

This issue makes me think that I'm missing something very basic about deallocating view controllers. Any help is appreciated.

enter image description here

SuperCodeBrah
  • 2,874
  • 2
  • 19
  • 33

1 Answers1

1

The problem is that I was calling pageSegue() from a modal VC. I originally had this:

@IBAction func goHome(_ sender: Any) {

    pageSegue("HomeScreen") 
}

Which I changed to this:

@IBAction func goHome(_ sender: Any) {

    dismiss(animated: false, completion: {
        pageSegue("HomeScreen")
    })
}

It's now deallocating the GameViewController each time the segue is called and CPU usage returns to 0% on the home screen.

SuperCodeBrah
  • 2,874
  • 2
  • 19
  • 33