Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS segue freeze for many seconds before showing new view

In the main view of my app I have a table view and two prototype cells. I have configured the segues for each cell using the storyboard. In the view controller I override prepareForSegue to pass information on the selected cell to the destination view.

The destination view isn't particularly complex and certainly doesn't require any heavy processing to load.

THE PROBLEM

When I tap on a cell in the main controller for the very first time, the destination view appears after a long delay, from 5 to 40 seconds.

EDIT #2: subsequent taps are generally faster

Note that:

  • If I tap on the same cell again before the destination view has appeared, this triggers the destination view to appear immediately.
  • As above, but tapping on a different cell results in the view appearing immediately but with the data from the first cell.
  • As above, but tapping on a different control (with no associated segues) triggers the destination view to appear immediately.
  • Subsequent "taps" generally manifest less delay.
  • The Time Profiler - for what I can see - shows that absolutely nothing is happening during those many seconds of delay.
  • I have tried different type of segues, but it made no difference
  • A few println's show that the following sequence of events occurs:

    • in the main view, prepareForSegue is executed (no delays)
    • then the destination viewDidLoad is executed (no delays)
    • ... long delay ...
    • the collection and table views in the destination controller start invoking the data source related methods to fetch the data from the controller.
    • the view finally appears (with an unwanted animation, BTW, but that's a different problem)

From what I have read on this topic, I suspect the problem is potentially related to some of the above operations happening in a background thread.

Any idea what I might be doing wrong?

EDIT #1: added some code

In the main view controller the segues have been link using the story board (CTRL-drag the two prototype cells into the destination view).

The code looks a bit like below:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    var assetIndex = assetsTable.indexPathForSelectedRow()?.row

    println("prepare for segue - start: \(assets[assetIds[assetIndex!]]!.Name)")

    if let destination = segue.destinationViewController as? AssetThingsListViewController
    {
        destination.bundlesRepository = bundlesRepository!
        destination.asset = assets[assetIds[assetIndex!]]
    }

    println("prepare for segue - end")
}

EDIT #3 I have made a sample project available on BitBucket

like image 905
Paolo Marini Avatar asked Jun 13 '15 21:06

Paolo Marini


People also ask

What is unwind segue iOS?

Unlike the segues that you use to present view controllers, an unwind segue promises the dismissal of the current view controller without promising a specific target for the resulting transition. Instead, UIKit determines the target of an unwind segue programmatically at runtime.

How do I segue to another view controller?

Well, in addition to that, Ctrl+dragging also helps set up segues. So, Ctrl+Drag from the “Go to Other View Controller” button, to somewhere in the second View Controller. It can be anywhere in the main box of the second view controller. When you release, it will show you a box like the one below.


1 Answers

I checked your project. And while I also couldn't find anything else, I also suspect that it's a problem with threading.

I managed to fix the problem by implementing a delegate for the tableview and present the new controller in code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let destination = storyboard?.instantiateViewControllerWithIdentifier("BuilderToysListViewController") as! BuilderToysListViewController
    destination.botsRepository = botsRepository!
    destination.builder = builders[builderIds[indexPath.row]]

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.presentViewController(destination, animated: true) { () -> Void in

        }
    })

}

Notice you have the set the view controller storyboard id: BuilderToysListViewController and also set the tableview delegate. Don't forget to remove the segues.

At the end to dissmis the view in the new view controller use this code:

@IBAction func backButton(sender: AnyObject)
{
    dismissViewControllerAnimated(true, completion: { () -> Void in

    })
//        performSegueWithIdentifier("segueToysByBuilder", sender: nil)        

}

This would allow you to properly close the view instead of wrongly creating a new one.

like image 89
pteofil Avatar answered Nov 02 '22 05:11

pteofil