如何在大型中央调度操作中运行异步操作? [英] How to run async operations within a grand central dispatch operation?

查看:78
本文介绍了如何在大型中央调度操作中运行异步操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要处理的作业队列,该队列由计时器定期踢,而且在将新作业添加到队列时也通过调用线程来踢.

I have a queue of jobs that need to be processed, the queue is periodically kicked by a timer but also by calling threads when a new job is added to the queue.

当队列被踢时,我想在另一个线程上启动队列的处理,因为我不想阻塞调用线程(在很多情况下,它将是UI线程).

When the queue is kicked I want to initiate the processing of the queue on another thread as I don't want to block the calling thread (Which in a lot of cases will be the UI thread).

为此,我在高优先级并发队列上运行了一个大型中央调度操作,这将创建我的http类的实例,并通过它提交作业(一个作业本质上是一个http请求).

So to do this I run a grand central dispatch operation on the high priority concurrent queue, this creates an instance of my http class and submits the job through it (A job is essentially an http request).

http类内部使用NSURLConnection执行异步请求.

The http class executes requests async using NSURLConnection internally.

我的问题是GCD操作完成了(因为它已经提交了所有异步http请求),所以我猜想它正在运行的线程已清理并重用或退出了.哪个正在清除正在执行这些异步Web请求过程中的http类.

My problem is the GCD operation finishes (As it has submitted all the async http requests) so then I guess the thread it was running on is either cleaned up and reused or exits. Which is wiping out my http classes that are in the process of doing these async web requests.

我的问题是,如何使我的http类挂起并完成对请求的处理,而无需让GCD操作等待它们?

My question is, how do I make my http class hang around and finish processing the request without making the GCD operation wait for them?

欢呼

推荐答案

异步http的主要目的不是阻止主线程,因为您正在通过操作队列使用后台线程,我想说它已经已搞定.因此,如果没有定义的处理结束后可以将数据取回的话,那么会缺少什么.

The main purpose of an asynch http would be not to block the main thread, since you are using a background thread through operation queue I'd say it is already taken care of. So what lacks if a defined end of processing where you can get your data back.

我想说,同步http会更简洁地设计并且更易于支持.您可以使用具有同步方法的NSURLConnection:

I'd say synchronous http would be cleaner to design and easier to support. You could use a NSURLConnection which has a synchronous method:

[NSURLConnection sendSynchronousRequest:returningResponse:error:]

我还刚刚找到了一个提供同步操作模式的库,并承诺使可可(包括可可触摸)网络编程更加容易: ASIHttpRequest

I also just found a library that provides synchronouse mode of operation and promises to make cocoa (incl. cocoa touch) networking programming much easier: ASIHttpRequest

这篇关于如何在大型中央调度操作中运行异步操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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