Reputation: 45
I have a problem with the memory of the phone. Im trying to do something like android Fragments, so I have a container view and when I need to change the view, I only set the UIViewController in the container, but every time I change the container view, the memory heap increase a lot, so in 8 clicks the memory on the iPhone goes to 640mb. How can I remove the viewControllers in the memory? this is my code:
private var activeViewController : UIViewController?{
didSet {
removeInactiveViewController(inactiveViewController:oldValue)
updateActiveViewController()
}
}
private func removeInactiveViewController(inactiveViewController: UIViewController?){
if let inactiveVC = inactiveViewController {
inactiveVC.willMove(toParent: nil)
inactiveVC.view.removeFromSuperview()
view controller
inactiveVC.removeFromParent()
}
}
private func updateActiveViewController(){
if let activeVC = activeViewController {
var duration = 0
addChild(activeVC)
activeVC.view.frame = fragmentContainer.bounds
fragmentContainer.addSubview(activeVC.view)
activeVC.didMove(toParent: self)
}
}
@IBAction func clickSearchs(_ sender: Any) {
if (statusFragments != 1 && statusFragments != 0){
prevStatusFragments = statusFragments
statusFragments = 1
imgSearch.isSelected = true
imgNotif.isSelected = false
imgRecord.isSelected = false
imgProfile.isSelected = false
imgOptions.isSelected = false
if (mapViewController != nil) {
print("Ya existia")
mapViewController!.removeFromParent()
mapViewController = nil
}
mapViewController = storyboard!.instantiateViewController(withIdentifier:
"MapContainerViewController")
activeViewController = mapViewController
} else {
}
}
@IBAction func clickNotifs(_ sender: Any) {
print("Hizo click en notificaciones")
if (statusFragments != 2){
prevStatusFragments = statusFragments
statusFragments = 2
imgSearch.isSelected = false
imgNotif.isSelected = true
imgRecord.isSelected = false
imgProfile.isSelected = false
imgOptions.isSelected = false
if (notifViewController != nil) {
notifViewController!.removeFromParent()
notifViewController!.removeFromParent()
notifViewController = nil
}
notifViewController = storyboard!.instantiateViewController(withIdentifier: "NotificationsContainerViewController")
activeViewController = notifViewController
} else {
}
}
Upvotes: 1
Views: 683
Reputation: 2147
Your current code looks good, please check if there is a retain cycle in the MapContainerViewController
or NotificationsContainerViewController
.
I recommend you to check all closures and variables defined in the view controllers.
Upvotes: 1