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:
A few println's show that the following sequence of events occurs:
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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With