Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chaining UITableview Updates on main thread

So what i am attempting to do is conceptually very simple however I have not been able to find a solution for it:

I am trying to remove cells from a tableView animated with the:

self.coolTableView?.deleteRowsAtIndexPaths

function, to do this I change the dataSet and perform this action, right after it is done i would like to change the data set again and use:

self.coolTableView?.insertRowsAtIndexPaths

to reflect and animate the second change to the dataset. The Problem I run into is that if I use:

dispatch_async(dispatch_get_main_queue()) { () -> Void in
//Update tableview
}

they seem to lock each other out, the used memory just keeps skyrocketing and it hangs. I am assuming they are interfering with each other. now my next thought was to change the code to sync so:

dispatch_sync(dispatch_get_main_queue()) { () -> Void in
//Update tableview
}

however the first update hangs and and ceases operation. With the research I have done it sounds like I am putting the execution of the block in the main queue behind my current running application and vuwala, that is why it hangs.

what is the correct way to block execution until I can complete an animation on the main thread so i do not blow up my data source before animations can take place?

like image 308
CWineland Avatar asked Feb 08 '26 03:02

CWineland


1 Answers

The animations in iOS take a block that they can execute when the animation terminates. Try putting operations on coolTableView into a closure (remember about unowned self to not create memory leaks) and pass it as completion closure to your animation.

example:

let someAnimations: () -> Void = {
  //some animations
}

let manipulateTableView: (Bool) -> Void = {
  isAnimationFinished in
  // perform some data manipulation
}

UIView.animateWithDuration(0.5, animations: someAnimations, completion:  manipulateTableView)
like image 70
66o Avatar answered Feb 09 '26 16:02

66o