gpt4 book ai didi

iphone - 当 dispatch_async 任务完成时如何通知我?

转载 作者:太空狗 更新时间:2023-10-30 03:23:58 27 4
gpt4 key购买 nike

我有一个这样的异步任务:

dispatch_async(dispatch_get_main_queue(), ^{
myAsyncMethodsHere;
});

有没有办法在后台任务完成时得到通知?

或者在完成时调用一个方法?

我已经通读了文档并查看了 dispatch_after,但它似乎更适合在一定时间后调度该方法。

感谢您的帮助。

最佳答案

来自文档:

完成回调

完成回调可以通过嵌套调用 dispatch_async() 函数来完成。这是重要的是要记住在第一次调用 dispatch_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

关于iphone - 当 dispatch_async 任务完成时如何通知我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3379008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com