Search code examples
iosswiftuitableviewuiviewcontrolleruimodalpresentationstyle

Swift Long Delay Between viewDidLoad and viewWillAppear


I'm encountering an problem where I am having a long delay when trying to present a ViewController. I am trying to display an upgrade alert when a user clicks on a UITableViewCell which requires premium access. In the ViewController being presented, I put debug code:

override func viewDidLoad() {
    super.viewDidLoad()

    println("\(NSDate()) viewDidLoad")
    // Set Navigation Title font and color
    self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "UbuntuCondensed-Regular", size: 22)!,
        NSForegroundColorAttributeName: UIColor.whiteColor()]
    println("\(NSDate()) end of viewDidLoad")
}

override func viewWillAppear(animated: Bool) {
    println("\(NSDate()) before super.viewWillAppear(animated)")
    super.viewWillAppear(animated)
    println("\(NSDate()) after super.viewWillAppear(animated)")
}

override func viewDidAppear(animated: Bool) {
    println("\(NSDate()) before super.viewDidAppear(animated)")
    super.viewDidAppear(animated)

    println("\(NSDate()) after super.viewDidAppear(animated)")
}

The println statement resulted in:

2015-06-23 16:36:54 +0000 viewDidLoad
2015-06-23 16:36:54 +0000 end of viewDidLoad
2015-06-23 16:36:57 +0000 before super.viewWillAppear(animated)
2015-06-23 16:36:57 +0000 after super.viewWillAppear(animated)
2015-06-23 16:36:58 +0000 before super.viewDidAppear(animated)
2015-06-23 16:36:58 +0000 after super.viewDidAppear(animated)

As you can see there is a 3 second delay between the end of viewDidLoad and the start of viewWillAppear. I can't figure out why this is occurring. I am creating the views programmatically within the ViewController, so the storyboard is not being used here.

This is the code I have to present my ViewController:

// Create the upgrade view contorller
let upgradeVC = UpgradeViewController()
// Set the presentation context
self.providesPresentationContextTransitionStyle = true
self.definesPresentationContext = true
// Set the upgrade controller to be modal over current context
upgradeVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
// Show the view controller
self.navigationController?.presentViewController(upgradeVC, animated: true, completion: nil)

Solution

  • Creating and presenting the view controller should be done on the main thread.

    (When view updates are delayed, it nearly always means you're erroneously working on a background thread.)