core-data icon indicating copy to clipboard operation
core-data copied to clipboard

Initial ViewController Loading twice?

Open tobhae-dev opened this issue 8 years ago • 1 comments

In AppDelegate the Main-Storyboard and initial VC is initiated and configured. Also in the App-Info.plist the App is configured to automatically load the Main-Storyboard [with its initialViewController].

So my Question: Is the Main-Storyboard and ViewController loaded twice?

tobhae-dev avatar Oct 29 '17 11:10 tobhae-dev

There are two instances of the UIStoryboard object (once initialized by the system and again programmatically in applicationDidFinishLaunching...). Both instances have the same values.

The initial view controller in Main.storyboard is a plain ol' UIViewController. I believe this is a placeholder view controller to display on screen while the Core Data stack is being set up.

The view controller with the identifier "RootViewController" that is instantiated in AppDelegate is the primary flow of the app. The window's root view controller starts off as the storyboard initial view controller but then is programmatically replaced.

        createMoodyContainer { container in
            self.persistentContainer = container
            self.syncCoordinator = SyncCoordinator(container: container)

// 2nd initialization of a storyboard object. Will be dealloc'd by end of closure.
// Could have done something like let sb = self.window?.rootViewController.storyboard, but
// that would have returned an optional as well as breaking the Law of Demeter guideline.
            let storyboard = UIStoryboard(name: "Main", bundle: nil)

// creating an instance of RootViewController
            guard let vc = storyboard.instantiateViewController(withIdentifier: "RootViewController") as? RootViewController else { fatalError("Wrong view controller type") }
            vc.managedObjectContext = container.viewContext

// Assigning the RootViewController instance to UIWindow's rootViewController property
// effectively "swaps in" the main UI/UX of the app.
            self.window?.rootViewController = vc

evandelaney avatar Jun 10 '18 18:06 evandelaney