I have an UITableViewController with a list of posts. When I press on a cell, I would display an alertController during the post image and post comments downloading. Then dismiss the alertController and push the UIViewController of the post (PostViewcontroller). PostViewcontroller has an UITableView for these comments.
Currently the behavior I have:
- press on a cell of my UITableViewController
- display the alertController
- downloading image and comments
- dismiss the alertController
- PostViewcontroller calls cellForRowAtIndexPath for each comments of the post. There are many comments so it's take a lot of time
- Finally UITableViewController pushes the PostViewcontroller
I would like dismiss the alertController just before pushing PostViewcontroller, after all the calls of cellForRowAtIndexPath.
The excepted behavior:
- press on a cell of my UITableViewController
- display the alertController
- downloading image and comments
- PostViewcontroller calls cellForRowAtIndexPath for each comments of the post. There are many comments so it's take a lot of time
- dismiss the alertController
- finally UITableViewController pushes the PostViewcontroller
In my UITableViewController:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var post : Post = posts[indexPath.row] as Post
//Display the alert controller during the image downloading
var alert = UIAlertController(title: "Alert", message: text, preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
// If the image does not exist, we need to download it
var imageUrl: NSURL! = NSURL(string: post.attachment!.imageUrl)
// Download an NSData representation of the image at the URL
let request:NSURLRequest=NSURLRequest(URL:imageUrl)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
if error == nil {
var image = UIImage(data: data)
CoreDataManager.sharedManager.addImageToCoreData(post, image:image!, type:"full")
//download the comments
self.comments=self.api.getCommentsData(post)
dispatch_async(dispatch_get_main_queue(), {
if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
//Image and comments downloaded, dismiss the alertcontroller
self.dismissViewControllerAnimated(true, completion: {
self.performSegueWithIdentifier("postview", sender: self)
})
}
})
}
})
}
I use prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "postview" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let destination = (segue.destinationViewController as! UINavigationController).topViewController as! PostViewController
destination.post=selectedPost!
destination.comments=comments!
destination.delegate=self
self.splitViewController?.toggleMasterView()
}
}
}
I tried to dismiss the alertController in prepareForSegue, but I have this error:
pushViewController:animated: called on <UINavigationController 0x78e43070> while an existing transition or presentation is occurring; the navigation stack will not be updated.
I don't know how to do and if it's possible. I missed something, but what?