I have a learning app where I chain multiple ViewControllers together using the function:
let main = UIStoryboard(name: "Main", bundle: nil)
weak var levelCompletedViewController = main.instantiateViewController(withIdentifier: "levelCompletedViewController") as? LevelCompletedViewController
levelCompletedViewController?.modalPresentationStyle = .fullScreen
self.present(levelCompletedViewController!, animated: false)
It always crashes after 24 viewControllers (I need 32) due to the error:
Message from debugger: Terminated due to memory issue
On the 32nd viewController, I would call dismiss() on all, such that the deinit function gets called. Also, each time in viewDidDisappear I delete all images and other memory-intensive variables.
My memory indicator shows that it never rises above 400MB. However, my application crashes every time exactly at 24 screens. (I want 32)
I use [weak self] in closures and have zombie objects disabled. and no infinite for loop or something similar, since the screen types are random and always crash at 24.
Is there a trick to fix this problem, like for example calling deinit every time I move from one viewController to another by resetting the root viewController or something similar?
EDIT:
As Sweeper mentioned in the comments, there would also be the possibility to just dismiss a screen before presenting another screen. Nevertheless, in this case, my custom transition from right to left would not work:
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromRight
self.view.window?.layer.add(transition, forKey: kCATransition)
@Ptit Xav thank you for that comment it really solved my problem.
All I did was:
var viewControllers = self.navigationController?.viewControllers
viewControllers?.remove(at: 1)
self.navigationController?.viewControllers = viewControllers ?? []