0

What I have

I don't understand the nature of the problem.

I have 2 View Controllers :

1) FeedViewController ,which shows events in tableview

2) EventViewController, pushed when you press on event.

When Feed Loads, it start asynchronous loadings of all images of all events. It's done for each event by following function:

    EventsManager().loadProfilePictureById(event.profilePictureID as String, currentProgress: event.profilePictureProgress, completionHandler: {
                        (progress:Double, image:UIImage!, error:NSError!) -> Void in
                        event.profilePictureProgress = progress
                        if image != nil {
                            event.profilePicture = image
                        }
                        if (error == nil){
                            if (self.tableView.headerViewForSection(index) != nil){
                                var header:eventHeaderView = self.tableView.headerViewForSection(index) as! eventHeaderView
                                header.updateProfilePicture(
                                    self.eventsManager.events[index].profilePictureID as String,
                                    progress: self.eventsManager.events[index].profilePictureProgress,
                                    image: self.eventsManager.events[index].profilePicture)
                            }
                        }else{
                            println("Error:" + error.description)
                        }
                    })

Here is how I push EventViewController:

    func PushEventViewController(sender:UITapGestureRecognizer)->Void{

    let ViewSender = sender.view!
    let selectedRow = ViewSender.tag
    //let Cell:HomeEventTableViewCell = TimelineEventTable.cellForRowAtIndexPath(SelectedIndexPath) as HomeEventTableViewCell
    dispatch_async(dispatch_get_main_queue(), {
        () -> Void in
        let VC:EventViewController = self.storyboard?.instantiateViewControllerWithIdentifier("EventViewController") as! EventViewController
        VC.event = self.eventsManager.events[selectedRow]
        self.navigationController?.pushViewController(VC, animated: true)
    })
}

Problem

The problem is that if I press on event and push EventViewController before image is downloaded (completion handlers are still getting called) it crashes app.

Assumptions

I struggled with this for days and couldn't find a solution, but my assumptions are that completion handler called after

Crash happens when it tries to execute following line after EventViewController was pushed:

                        event.profilePictureProgress = progress
                        if image != nil {
                            event.profilePicture = image
                        }

I assume when new view controller is pushed , event object which is used in completion handler is being deallocated

  • 1
    If you have the crash error message, post it and it helps a lot. Or you can read this: http://blog.manbolo.com/2012/01/23/xcode-tips-1-break-on-exceptions and to set a break point for exception and then you will know what cause the crash. – Surely Sep 05 '15 at 15:42
  • @xWhyLikeThis I have updated the question. – Grisha Gevorkyan Sep 05 '15 at 15:58

1 Answers1

1

Found out where the problem was, the issue was that variable event.profilePictureProgress was declared as dynamic var (I was going to take advantage of that to add observer to it after).