4

I have a long running tasks that will run on a background queue, serially.

One special requirement is that I want the max queue size to be 1, and for it to be LIFO.

So, if N items come in while a job is running, I want them all to be dropped EXCEPT the most recent one. Similar to how a "debounce" works.

2
  • Would a max-heap work for your purposes? Commented Jun 22, 2016 at 17:13
  • you can cancelAllOperations in your operationQueue before adding any new blockOperation: [operationQueue cancelAllOperations]; [operationQueue addOperation:myblockOperation]; Commented Jun 22, 2016 at 18:34

1 Answer 1

3

This type of thing is usually done with a DISPATCH_SOURCE_TYPE_DATA_OR source in GCD. That coalesces multiple requests (triggered via dispatch_source_merge_data()) into a single invocation of the source event handler.

It is restricted to 64 bits of "payload" retrievable with dispatch_source_get_data(), (atomically ORed together from all the values passed to merge_data() that were coalesced into this specific invocation of the handler).

Alternatively a DATA_ADD source can be used to simply count how many requests were coalesced into a single handler invocation.

If requests come in while an event handler is already being executed, the event handler will immediately be run again as soon as the previous invocation finishes (to deliver all the requests that were coalesced during the execution of the previous handler).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.