NSURLConnection的和多个异步请求 - 是它搞乱与正在传输的数据? [英] NSURLConnection and multiple asynchronous requests - is it messing with the data being transmitted?

查看:148
本文介绍了NSURLConnection的和多个异步请求 - 是它搞乱与正在传输的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的NSArray 链接。我想通过它们与在线文章提取API解析(清除读的),并且其结果定回每篇文章(一些HTML),我把它变成一个的NSString

I have an NSArray of links. I want to parse through them with an online article extractor API (Clear Read), and with the result given back for each article (some HTML) I throw it into an NSString.

我的问题从事实的出现,说我的阵列有100个网址中,我遍历数组拍摄的每个项目到API并取回一个JSON一些成果。这是射击像100 NSURLConnection的调用一次异步。

My problem arises from the fact that, say my array has 100 URLs in it, I loop through the array shooting each item into the API and getting back some results in JSON. This is firing like 100 NSURLConnection calls at once asynchronously.

我不知道这会是一个问题,但是当我给它100个URL(真正的字符串,没有一个是零),当他们不该'那回来常为JSON键有要么是空值(数据T),或回来是数据。还有一堆重复的。

I wasn't sure if that'd be a problem, but when I give it 100 URLs (real strings, none are nil) the data that comes back often has either empty values for the JSON keys (when they shouldn't), or the data coming back is nil. There's also a bunch of duplicates.

我应该处理多个异步连接比我现在?如果是这样,怎么样?

Should I be handling multiple asynchronous connections better than I am now? If so, how?

推荐答案

一对夫妇的想法:


  1. 如果你正在做的并发异步请求,并采用异步 NSURLConnection的,那么你要定义你自己的类此下载操作,以确保每个连接跟踪其自身的属性。这样一来,一切都可以在这个类中被封装,其中所产生的下载对象可以的,如果你不使用异步跟踪什么的下载,什么是被解析等 NSURLConnection的(例如,你仅仅使用 dataWithContentsOfURL ),那就更简单了,虽然你失去了一些进展更新 NSURLConnection的提供和/或数据流的机会。

  1. If you're doing concurrent asynchronous requests and are using asynchronous NSURLConnection, then you'll want to define your own class for this download operation to make sure that every connection keeps track of its own properties. That way, everything can be encapsulated within this class where the resulting download objects can keep track of what's downloaded, what's been parsed, etc. If you're not using asynchronous NSURLConnection (e.g. you're just using dataWithContentsOfURL), it's even easier, though you lose some of the progress updates that NSURLConnection provides and/or streaming opportunities.

为了获得最佳性能,你应该做的并发请求。说了这么多,你不应该去任何特定的服务器超过四或五个并发请求。这是一款iOS强加的约束,特别是如果你有一个缓慢的网络连接,你可能有连接超时,否则。

For best performance, you should do concurrent requests. Having said that, you should not have more than four or five concurrent requests going to any particular server. This is an iOS imposed constraint, and especially if you have a slow network connection, you risk having connections timeout otherwise.

如果你在模拟器上做preliminary测试,您可能希望确保您尝试了网络连接空调。这是硬件IO工具X code的一部分,可在下载为苹果开发者。有问题(如前面提到的超时问题,如果你有去一个特定的服务器太多的并发请求),只有体现在连接速度慢。

If you're doing preliminary testing on the simulator, you may want to make sure you try out the "network link conditioner". It's part of the "Hardware IO Tools for Xcode", available at the Downloads for Apple Developers. There are issues (such as the aforementioned timeout problems if you have too many concurrent requests going to a particular server) that only manifest themselves in slow connections.

说了这么多,你还需要确保测试你与真实世界的网络速度的设备上的解决方案。这很容易成功成功的过于贪婪设备的模拟器上运行大规模并行任务。限制并发会话数到五会减少这种资源的问题,但它应该是你的测试策略的一部分。

Having said that, you also want to make sure to test your solution on a device with real world network speeds. It's easy to successfully run massively parallel tasks successfully on the simulator that are too greedy for the device. Limiting the number of concurrent sessions to five will diminish this resource problem, but it should be part of your testing strategy.

我同意JRG开发者,你应该考虑建立的框架,比如 AFNetworking 。请务必将 maxConcurrentOperationCount 队列 AFHTTPClient ,不过,如果排队100多操作。

I agree with JRG-Developer, that you should look into established frameworks, such as AFNetworking. Make sure to set the maxConcurrentOperationCount for the queue of the AFHTTPClient, though, if queueing 100 plus operations.

我不知道你的100请求多少数据意味着,但事先警告该应用程序的审批程序已经知道拒绝的应用程序,使蜂窝网络非凡的网络请求。怎样才算过度的蜂窝网络活动没有在应用程序中审查指南明确指出,虽然避免iPhone应用程序拒绝从苹果声称,你应该确保你没有在5分钟内超过4.5MB比多。您可以使用<一个href=\"http://developer.apple.com/library/ios/#sample$c$c/Reachability/Introduction/Intro.html\">Reachability确定你是什么类型的网络,或许警告用户,如果他们是在蜂窝(如果数据量接近这个门槛)。

I don't know how much data your 100 requests entail, but be forewarned that the app approval process has been known to reject apps that make extraordinary networks requests on cellular networks. What constitutes excessive cellular network activity is not explicitly stated in the app review guidelines, though Avoiding iPhone App Rejection From Apple has claimed that you should ensure that you don't exceed more than 4.5mb in 5 minutes. You can use Reachability to determine what type of network you are on and perhaps warn the user if they're on cellular (if the amount of data approaches this threshold).

这篇关于NSURLConnection的和多个异步请求 - 是它搞乱与正在传输的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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