Search code examples
iosobjective-cswiftsegue

performSegueWithIdentifier very slow when segue is modal


I have a simple table view where I handle the select action on the table view. This action follows a segue.

If the segue is a push segue, the next view shows immediately. If the segue is a modal segue, the next view either:

  • takes 6 seconds or so to display
  • shows immediately if I tap again (second tap)

I tried looking around for some ideas, but none seem applicable to my situation. In particular:

  • I'm performing the segue on the main UI thread
  • My view is very simple (so there's no issue in viewDidLoad). Plus the fact that it shows up near instantaneous when the segue is push indicates that there is no problem loading the target view
  • I tried passing nil to the sender; same effect.

Does anyone have any ideas on this?


Solution

  • Trust me and try this. I have run into this problem a few times.

    In Swift 2:

    dispatch_async(dispatch_get_main_queue(),{
        self.performSegue(withIdentifier:mysegueIdentifier,sender: self)
    })
    

    or for Swift 3:

    DispatchQueue.main.async {
        self.performSegue(withIdentifier: mysegueIdentifier,sender: self)
    }
    

    As discussed here and here.