0

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:

  1. press on a cell of my UITableViewController
  2. display the alertController
  3. downloading image and comments
  4. dismiss the alertController
  5. PostViewcontroller calls cellForRowAtIndexPath for each comments of the post. There are many comments so it's take a lot of time
  6. Finally UITableViewController pushes the PostViewcontroller

I would like dismiss the alertController just before pushing PostViewcontroller, after all the calls of cellForRowAtIndexPath.

The excepted behavior:

  1. press on a cell of my UITableViewController
  2. display the alertController
  3. downloading image and comments
  4. PostViewcontroller calls cellForRowAtIndexPath for each comments of the post. There are many comments so it's take a lot of time
  5. dismiss the alertController
  6. 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?

cmii
  • 3,556
  • 8
  • 38
  • 69

1 Answers1

0

You can't force a view controller's table view to load it's cells before the view controller itself is loaded.

You can change the behavior to the following:

  1. press on a cell of my UITableViewController
  2. push the PostViewcontroller
  3. display the alertController (from inside the PostViewController)
  4. downloading image and comments
  5. tell PostViewController's tableView to reloadData (It will only load the cells that are required to fill the display so it won't take a lot of time.)
  6. dismiss the alertController

Another option would be to have each tableViewCell download its own image and comment. It can show an activity indicator while the download is taking place and then show the data once the download is finished.

Apple has sample code that demonstrates this: https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html

Daniel T.
  • 32,821
  • 6
  • 50
  • 72