开发者

How to run async operations within a grand central dispatch operation?

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.

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).

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).

The http class executes requests async using NSURLConnection internally.

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.

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?

Cheers


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.

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:]

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜