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
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: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:
This would allow you to properly close the view instead of wrongly creating a new one.