-1

I found that my controller is out of the view hierarchy in this code andI call this in viewDidLoad:

if CLLocationManager.locationServicesEnabled() {
    // code
} else {
    let alertController = UIAlertController(...)
    //
    present(alertController)
}

If I wrap else clause in .async or .asyncAfter in the main queue I have my issue go away. Why do this happen here?

Thank you!

wm.p1us
  • 2,019
  • 2
  • 27
  • 38

1 Answers1

1

based on your question you are tried to load UIAlertController before load the UIviewcontroller hierarchy, in here you can do it two ways,

  • you can forcefully load the UIAlertController in the main thread, so in here you need to use .async or .asyncAfter, but it's not suggested.
  • alternate suggestion but it will works fine, you need to wait for your initial UI view hierarchy, I mean you need to convert your code from viewDidLoad to viewDidAppear. for ref : Difference between viewDidLoad and viewDidAppear
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • "load the UIAlertController in the main thread". What does that even mean? You cannot use `viewWillAppear. You have to use `viewDidAppear` because otherwise `self` is not in the view hierarchy yet and there is no way to present a controller outside the view hierarchy. – Sulthan Feb 15 '19 at 08:08