开发者

Workflow with reloadRowsAtIndexPaths:withRowAnimation

I'm trying to dynamically update cells in a UITableView. The events are triggered by data received by background threads, so everything is asynchronous and it's impossible to predict the order of the events.

I'm not sure what the workflow is supposed to be in this case. Ho开发者_StackOverflow中文版w can I make sure that the row count and indexpath are correct when calling reloadRowsAtIndexPaths:withRowAnimation? There could still be an animation going on by a previous call to deleteRowsAtIndexPaths:withRowAnimation.

If all the changes happen at the same time, I can use beginUpdates and endUpdates, but what do I do in the above situation?


Hanno, I recommend using a command object model to queue the changes on the main thread.

Here's how:

  • On the callback in the main thread, append the add or delete as an object to a queue (mutable array).
  • Walk the queue to add and remove objects on the main thread, with each operation being atomic (i.e gets the table status first, processes it without interruption, removes the head of the queue)
  • Decide if you want to interrupt the animation if there are more commands on the queue, or wait for the animation to end then process the next queued object.

A mix of a timer and animationDidFinish callbacks should do it.


Since you would have to do all the table updates in the main thread anyway (because UIKit has to be called on the main thread), I don't see a concurrency problem here. Whenever you receive new data, call a method on the main thread that updates the table. This method will block the main thread until it has finished. This should make sure that the table view retains its integrity.

Or are you concerned about overlapping row animations?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜