在单独的线程上使用异步URL请求?

时间:2022-10-13 13:28:46

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.

我是IOS开发和objective-c的新手,并且没有重写我们的操作队列如何处理网络请求,我很想知道我的方法的任何替代方案。

1 个解决方案

#1


6  

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.

出于兴趣,您是否考虑过查看ASIHTTPRequest库,这是一个很好的开源CFNetwork包装器集?它具有一些非常有用的异步网络功能,包括能够指定超时并传入要在完成和/或失败时执行的块。

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.

听起来它可以为您节省很多痛苦,特别是如果您是Objective-C和iOS的新手 - 无需重新发明*,它可以显着简化网络操作。我们(我工作的公司)现在在绝大多数构建中使用它。它非常稳定且非常实用 - 我还没有从我们与该库相关的分析中报告错误,而且这是在数百万个会话中报告的。我不能推荐它来简化这些类型的网络任务。

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.

既然你问过你的方法的潜在替代方案,我想我会建议它,虽然我明白你是否不愿意在你的项目中引入另一个库或者无法使用OSS代码。

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

编辑:如果我为您提供链接会有所帮助! http://allseeing-i.com/ASIHTTPRequest/

#1


6  

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.

出于兴趣,您是否考虑过查看ASIHTTPRequest库,这是一个很好的开源CFNetwork包装器集?它具有一些非常有用的异步网络功能,包括能够指定超时并传入要在完成和/或失败时执行的块。

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.

听起来它可以为您节省很多痛苦,特别是如果您是Objective-C和iOS的新手 - 无需重新发明*,它可以显着简化网络操作。我们(我工作的公司)现在在绝大多数构建中使用它。它非常稳定且非常实用 - 我还没有从我们与该库相关的分析中报告错误,而且这是在数百万个会话中报告的。我不能推荐它来简化这些类型的网络任务。

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.

既然你问过你的方法的潜在替代方案,我想我会建议它,虽然我明白你是否不愿意在你的项目中引入另一个库或者无法使用OSS代码。

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

编辑:如果我为您提供链接会有所帮助! http://allseeing-i.com/ASIHTTPRequest/