3

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?

CWineland
  • 615
  • 7
  • 18
  • Are you familiar with the process of updating your table view with `beginUpdates` and `endUpdates` `UITableView` methods? The [accepted answer to this question](http://stackoverflow.com/questions/5429960/how-can-i-insert-row-in-a-table) might hold the clues you need to find a solution. Also as a side note you should, as a design principle, maintain all UI interactions on the main thread. – andrewbuilder Oct 12 '15 at 20:35
  • yes i have those locks around the actual animation calls of insert and delete row but that does not stop me from clobering the backing data needed to perform the animations – CWineland Oct 12 '15 at 20:41
  • Block execution in your data source on another thread? – quellish Oct 15 '15 at 07:57

1 Answers1

1

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)
66o
  • 756
  • 5
  • 10
  • this would work if my animations were centralized, however if i was just doing an incert and a delete right next to each other the default locks around modifying a tableview would work. If there was a way to add a completion block to dispatch_async this idea could work, looking into it now – CWineland Oct 12 '15 at 20:43