I have some scenarios in my app in which the navigation stack becomes very long because of the number of UIViewControllers
I push. I am building something similar to a file explorer UI in my app and there is a chance lots of UIViewControllers
will be pushed in the navigation stack. For very deep navigation stack I run into memory warnings.
In the app, we need to only show the current UIViewController
all previous UIViewConrollers
are in memory. Can we optimize this and add some sort of virtualization to improve memory footprint ?
As far as I understand you have UIViewController
for each folder.
I think there is no problem if you keep just the last 3 controllers. And on viewWillApear
of each UIViewController
you have a check for older view controllers and to remove some view controllers
Maybe something like this in viewWillAppear
if (navigationController?.viewControllers.count)! > 3 {
navigationController?.viewControllers.removeFirst()
} else if (navigationController?.viewControllers.count)! < 2 {
if true {
let olderPage = UIViewController()
navigationController?.viewControllers.insert(olderPage, at: 0)
}
}
For sure you can play more with the stuff that you have in the View Controller and initialize as much as you can in viewWillApear
and free as much as you can in viewWillDisapear
. Have in mind that if you load images with UIImage(named:)
image will be cached and they will not dealoc the memory.