0

I have a FakeSplashController which does some network requests and after that open WelcomeViewController. When I look memory graph in WelcomeViewController, I see that SplashViewController is still in there. I'm calling deinit function in FakeSplashController to check If It is deniniting but It doesn't call it. What can be the problem?

What I see in Memory when in WelcomeViewController: enter image description here

FakeSplashController:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    setupUI()
    networkRequests()

}

func networkRequests(){

    AppInitService().initAppRequest { [](result) in
        switch result{
        case .success(_):
            self.startAnimation()
            return
        case .error(let error):
            UIControlUtil.showErrorMessage(title: error.title, message: error.message, closeButton: true)
            return
        }
    }
}

func openApp(){
        let loginController = WelcomeViewController()
    self.present(loginController, animated: true)
}

func startAnimation(){
    UIView.animate(withDuration: 0.8, animations: {
        self.logoImage.frame.origin.x -= 100
    }, completion: nil)
    UIView.animate(withDuration: 1,delay: 0.3,animations: {
        self.textLogo.alpha = 1
        self.textLogo.frame.origin.x += 50
    }, completion: { _ in
        self.openApp()
    })
}

deinit {
    print("Splash Deinited")
}

EDIT: As I red in below post, someone mentioned that;

Unless you're doing something very specialized, you don't need to de-init an object in Swift. It will be called automatically when the reference count goes to 0. If you really need to, you should be able to set you window's rootViewController through your AppDelegate.

So don't I need to take this as a problem? Best way to deinit Initial view controller?

4
  • How do you expect splash screen to be deallocated since you present WelcomeViewController ? Commented Jun 12, 2018 at 15:07
  • I think I'm not sure If it needs to be. Since I dont have anything to do with splash anymore. Commented Jun 12, 2018 at 15:09
  • A view controller is not deallocated when you present another one; it still lives under new one. In fact, if you dismiss WelcomeViewController you'll see the splash again Commented Jun 12, 2018 at 15:12
  • @RicoCrescenzio is right, you can also see that, when you open the ViewDebugger of Xcode. Scroll a little to the sides in the ViewDebugger you will see, that behind your WelcomeViewController is the SplashViewController. When you would dismiss the WelcomeViewController, you would see the SplashViewController as well. Commented Jun 12, 2018 at 19:17

1 Answer 1

3

As I said in the comment, there's nothing strange in your code, it's how the iOS framework works; the view controller is not deallocated because it's still the rootViewController of the current window. I don't think it's very needed to deallocate it, but if you really need to do it, a solution could be to replace the rootViewController with your WelcomeViewController. By replacing it, the reference count of the splash screen will go to 0 and it will be deallocated. Something like

guard let window = UIApplication.shared.keyWindow else {
    return
}
self.dismiss(animated: false, completion: nil)
window.rootViewController = WelcomeViewController()

The problem of this is that there's no animation (but you can achieve the animation too, just find how to animate rootViewController change)

Sign up to request clarification or add additional context in comments.

8 Comments

Just one more question. For example I have a ViewController which store 10mb on memory. When I didn't deallocate it (like doing in my code), It will still use 10 mb on memory or will be deallocated?. I'm not sure which one I do need to use for splashController
It will be alive, so it will keep using 10 mb until its reference count reaches 0.
So It is better to use your code for SplashViewController and even WelcomeViewController cuz It will not be seen till user logout?
Maybe it’s better in terms of memory usage, but I would not worry about 2 simple view controllers; then I would have to re-implement the push animation to make the navigation consistent
Hmm maybe you will be true. One solution is to keep it and other one is to make a custom animation for push? It would be great If there is more Information about keeping vs deiniting it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.