Unfortunately the dispatch_block_testcancel() doesn't behave as expected. It just return whether or not you called dispatch_block_cancel() on a block, not if the block was successfully cancelled before being dispatched to a queue.
If the behavior of dispatch_block_testcancel() changes, the solution would look as follows:
func cancel() -> Bool {
dispatch_block_cancel(block) // Cancel block
let success = dispatch_block_testcancel(block) // Test if block was successfully cancelled
return success != 0
}
Unfortunately the
dispatch_block_testcancel()
doesn't behave as expected. It just return whether or not you calleddispatch_block_cancel()
on a block, not if the block was successfully cancelled before being dispatched to a queue.If the behavior of
dispatch_block_testcancel()
changes, the solution would look as follows: