完成回调
完成回调可以通过对dispatch\u async()函数的嵌套调用来完成。它是
重要的是要记住在第一次调用dispatch\u async()之前保留目标队列,并在完成回调结束时释放该队列,以确保在完成回调挂起时不释放目标队列。例如:
void
async_read(object_t obj,
void *where, size_t bytes,
dispatch_queue_t destination_queue,
void (^reply_block)(ssize_t r, int err))
{
// There are better ways of doing async I/O.
// This is just an example of nested blocks.
dispatch_retain(destination_queue);
dispatch_async(obj->queue, ^{
ssize_t r = read(obj->fd, where, bytes);
int err = errno;
dispatch_async(destination_queue, ^{
reply_block(r, err);
});
dispatch_release(destination_queue);
});
}
Source