怎样才能当dispatch_async任务完成后通知我? [英] How can I be notified when a dispatch_async task is complete?

查看:188
本文介绍了怎样才能当dispatch_async任务完成后通知我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个异步任务,像这样:

I have a asynchronous task like so:

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

有没有当后台任务完成后得到通知的方式?

Is there a way to be notified when the background task is complete?

还是要完成时调用的方法?

Or to call a method upon completion?

我已经通过的文​​件读取,并看着dispatch_after,但它似乎更设计了一定时间后,调度方法。

I've read through the documentation and have looked into dispatch_after, but it seems to be more designed to dispatch the method after a certain length of time.

感谢您的帮助。

推荐答案

从文档:

完成的召回事件

完成回调可以经由嵌套调用到dispatch_async()函数来完成。它是
重要的是要记住,保留目标队列第一次调用之前dispatch_async(),并释放该队列在完成回调的结束,以确保同时完成回调正在等待目标队列不释放。例如:

Completion callbacks can be accomplished via nested calls to the dispatch_async() function. It is important to remember to retain the destination queue before the first call to dispatch_async(), and to release that queue at the end of the completion callback to ensure the destination queue is not deallocated while the completion callback is pending. For example:

 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);
         });
 }

<一个href=\"http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man3/dispatch_async.3.html\">Source

这篇关于怎样才能当dispatch_async任务完成后通知我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆