GCD: How to remove waiting tasks from serial queue?

后端 未结 3 855
一整个雨季
一整个雨季 2021-02-13 21:39

First I create a serial queue like this

static dispatch_queue_t queue = dispatch_queue_create(\"myQueue\", DISPATCH_QUEUE_SERIAL);

then, at som

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-13 22:04

    Once a block has been submitted to a GCD dispatch queue, it will run. There is no way to cancel it. You can, as you know, implement your own mechanism to "abort" the block execution early.

    An easier way to do this would be to use NSOperationQueue, as it already provides an implementation for canceling pending operations (i.e., those not yet running), and you can easily enqueue a block with the new-ish addOperationWithBlock method.

    Though NSOperationQueue is implemented using GCD, I find GCD much easier to use in most cases. However, in this case, I would seriously consider using NSOperationQueue because it already handles canceling pending operations.

提交回复
热议问题