Search code examples
iosswiftuiviewcontrollerautomatic-ref-countinguiwindow

How efficient is changing window root viewcontroller?


Im making an app which the user can log in to and log out. I check if a saved user exists (from earlier logins) and if a user is saved I log that user in and then change rootviewcontroller for the window to the viewcontroller that should appear when logged in.

// In LoginViewController
let sb = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) // Login viewcontrollers as login, facebook login, register is inside its own storyboard called Login
let vc = sb.instantiateInitialViewController() as MainViewController

let window = UIApplication.sharedApplication().delegate!.window!!

window.rootViewController = vc

But Im just wondering, how efficient is this?

Will it handle the memory properly by just doing this?


Solution

  • It does not do anything special. It is similar to presenting/dismissing view controller.

    The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

    It will handle memory properly as long as you handle memory properly (no retain cycles between views, view controllers, delegates etc.).

    Of course detailed numbers can be found using time, allocations profiling instruments (as @Ian MacDonald suggested)

    Off topic a bit: as for me it is a preferred way to switch between logged-in/logged-out states of the app. I believe this approach leads to simplified screen navigation between logged-in/logged-out state: once you log in you change root vc to your data controller and forget about login stuff.