GCD: How to remove waiting tasks from serial queue?

后端 未结 3 854
一整个雨季
一整个雨季 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:11

    The way you handle this is to use an ivar that indicates to the queued blocks they should just return:

    ^{
      if(!canceled) {
        ... do work
      }
    }
    

    You don't need to use a simple boolean either - you can make this more complex - but the general idea is to use one or more ivars that the block queries before doing anything.

    I use this technique (but did not invent it) with great success.

提交回复
热议问题