9

I am trying to re-schedule queued block that will handle the update operations. Main goal is updating UI objects (online user table...) with minimum amount of (UI update request). (Server sometimes rain down massive amount of updates, yay!)

For simplicity main scenario is;

  • The dispatch_queue_t instance (queue that will handle given UI updating block) is a serial dispatch queue (private dispatch queue)

  • The operation (UI updating block) is scheduled with dispatch_after with t amount of time (Instead of updating for each data set update, collect update requests within t amount of time and perform a single UI update for them)

  • In case our data set updated, check if there already exist a scheduled event. If yes, unschedule it from dispatch_queue_t instance. Then re-schedule same block with t amount of time delay.

Also;

t is a small amount of time interval that possibly won't be noticed by the user (like 500 ms.) Any alternative approach is welcome.

My motive behind this;

i applied same logic via Android's Handler (post & removeCallbacks combination with Runnable instance) and i hope i could achieve the same on iOS.

Edit:

As @Sven suggested usage of NSOperationQueue is more suitable for the scenario as they support cancelling each NSOperation. I skimmed through documents and found;

Canceling Operations Once added to an operation queue, an operation object is effectively owned by the queue and cannot be removed. The only way to dequeue an operation is to cancel it. You can cancel a single individual operation object by calling its cancel method or you can cancel all of the operation objects in a queue by calling the cancelAllOperations method of the queue object.

You should cancel operations only when you are sure you no longer need them. Issuing a cancel command puts the operation object into the “canceled” state, which prevents it from ever being run. Because a canceled operation is still considered to be “finished”, objects that are dependent on it receive the appropriate KVO notifications to clear that dependency. Thus, it is more common to cancel all queued operations in response to some significant event, like the application quitting or the user specifically requesting the cancellation, rather than cancel operations selectively.

4

2 回答 2

14

这也可以通过 GCD 轻松完成,无需在此处使用 NSOperationQueue 的大锤子。

只需直接使用非重复调度计时器源而不是dispatch_after(这只是此类计时器源的便利包装,它实际上不会将块排入队列,直到计时器关闭)。

您可以使用 重新安排挂起的计时器源执行dispatch_source_set_timer()

于 2013-02-21T20:28:19.553 回答
6

您不能删除或以其他方式更改调度队列中排队的操作。NSOperationQueue尝试使用支持取消的更高级别。

于 2013-02-21T15:45:19.847 回答