Alarming memory increase with custom segue

663 Views Asked by At

I'm creating an app with XCode 6 (Swift) and recently managed to make a custom segue that doesn't use an animation. What I soon realised was that, when switching between two view controllers (tied together with the custom segues), the application memory usage (viewable with XCode) seemed drastically increase with every view controller switch. Is this something that I need to worry about? Does Swift automatically take care this memory increase, so that the app doesn't end up using an insane amount, or am I doing something wrong?

NoAnimationSegue.swift

import Foundation
import UIKit

@objc(NoAnimationSegue)
class NoAnimationSegue: UIStoryboardSegue {

override func perform () {
    let src: UIViewController = self.sourceViewController as UIViewController
    let dst: UIViewController = self.destinationViewController as UIViewController

    src.presentViewController(dst, animated: false, completion: nil)

}
}
1

There are 1 best solutions below

3
On BEST ANSWER

When you go from the first view controller to the second, you can presentViewController, but when you go back to the first, you do not want to "present" again. When you "present" one view controller from another, it keeps the first one in memory, so it's there, ready for you when you "dismiss" the second view controller to return back to the first.

Thus, if you repeat that process of presenting from one view controller to another in a circular fashion, you'll end up with multiple instances of the view controllers in memory. If you "present" to go from one view controller to another, then you'll want to unwind or dismiss to get back to the first view controller.

Alternatively, if you don't want to use the present/dismiss pattern, you can alternatively use tab bar controller if you want to jump back and forth between view controllers. Or you can use page view controller. Or you can use your own custom container view controller and replace the child view controller as you jump back and forth.

But just remember, if you present or push from one view controller to another, then you'll have to dismiss or pop to return back.