开发者

Using Asynchronous URL Requests on a separate thread?

I'm trying to replace a synchronous network call with an asynchronous one. The issue is that we need to timeout relatively quickly, and that's nearly impossible to do with a sync network connection.

One problem is the code is开发者_运维技巧 not structured to support asynchronous network calls, so to do this, I'd like to "simulate" a synch call by creating a separate thread that makes the async call, and in the thread call, check to see if the call has finished, or wait for a timeout, whichever is occurs first. If the timeout occurs, cancel the network request.

If I create a connection in a new thread, and that thread exists, will my connection end?

I'm new to IOS development and objective-c, and short of rewriting how our operationqueue handles network requests, I'd love to hear about any alternatives to my approach.


Out of interest, have you thought about looking at the ASIHTTPRequest library, a nice open source set of CFNetwork wrappers? It has some very useful asynchronous networking abilities, including being able to specify timeouts and pass in blocks to be executed upon completion and/or failure.

It sounds like it might save you a lot of pain, especially if you're new to Objective-C and iOS - no need to reinvent the wheel, and it simplifies network operations significantly. We (the company I work for) use it now in the overwhelming majority of our builds. It's highly stable and extremely usable - I have yet to get a bug reported back from our analytics associated with the library, and that's across millions of sessions. I can't recommend it enough to simplify these kinds of networking tasks.

Since you asked for potential alternatives to your approach I thought I'd suggest it, although I understand if you're unwilling to introduce another library or unable to use OSS code in your project.

Edit: would help if I provided you with a link! http://allseeing-i.com/ASIHTTPRequest/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜