简体   繁体   English

NSOperation队列与NSUrlConnection异步

[英]NSOperation Queue vs NSUrlConnection async

I have an app that is downloading several photos off of Flickr. 我有一个应用程序正在从Flickr下载几张照片。 Right now, all the photos are downloaded with a custom NSOperation class run on an NSOperationQueue .However, I have heard about NSUrlConnection async being more efficient, and was wondering which is better of this situation? 现在,所有照片都是通过在NSOperation运行的自定义NSOperation类下载的NSOperationQueue但是,我听说NSUrlConnection异步效率更高,想知道这种情况哪种更好? Or, is there a third option that's even better than these two? 或者,还有第三种选择比这两种选择更好吗? The custom NSOperation simply calls [NSData dataWithContentsOfURL:] many times on different photos. 定制的NSOperation只是在不同的照片上多次调用[NSData dataWithContentsOfURL:]

Using an approach which utilizes a subclass of NSOperation and which encapsulates NSURLConnection which is used in asynchronous mode (implementing the delegate protocols) is likely the most efficient, iff you additionally consider these aspects: 如果您另外考虑以下方面,则使用一种利用NSOperation的子类并封装NSURLConnection的方法可能是最有效的方法,该方法用于异步模式(实现委托协议)。

Ensure that the NSOperation subclass handles the delegate methods quickly , and that the underlaying thread (or the queue) will NOT be used to process the response data. 确保NSOperation子类可以快速处理委托方法,并且不要将底层线程(或队列)用于处理响应数据。 Ideally, the delegate methods pass over the partial response data to another queue or thread where they are processed (note: image data may be preloaded on a background thread or a queue!). 理想情况下,委托方法将部分响应数据传递到另一个队列或线程,并在其中进行处理(注意:图像数据可能会预加载到后台线程或队列中!)。

The reason for this is that, the sooner the network operation finishes, the more requests can be performed per time. 其原因是,网络操作越早完成,每次可以执行的请求越多。 The network NSOperation shall be put into a NSOperationQueue whose max concurrent operations is set to 1, or 2. Rarely to 4 or higher. 网络NSOperation必须放入NSOperationQueue中,该队列的最大并行操作数设置为1或2。很少是4或更高。 This setting depends on whether the server supports pipelining, and on the speed of the connection. 此设置取决于服务器是否支持管道传输以及连接速度。 Name that queue "Network bound queue". 将该队列命名为“网络绑定队列”。

The "data process" (preload image data) task is ideally a subclass of NSOperation, too. 理想情况下,“数据处理”(预加载图像数据)任务也是NSOperation的子类。 Likewise, the "data process" operations should be queued in a CPU bound NSOperationQueue. 同样,“数据处理”操作应在CPU绑定的NSOperationQueue中排队。 Per default the max concurrent operations of a NSOperationQueue is already suitable for CPU bound operations. 默认情况下,NSOperationQueue的最大并发操作数已经适合于CPU绑定的操作。

If you want to save the data to disk, again, ideally you create a NSOperation and queue those disk operations in a "disk bound queue". 如果您想将数据保存到磁盘,理想情况下,您最好创建一个NSOperation并将这些磁盘操作放入“磁盘绑定队列”中。 On devices, this seems not necessary, but if you have still such oldish "disks" - than it makes sense to set the number of max concurrent operations to the number of independent heads of the disk. 在设备上,这似乎不是必需的,但是如果您还有这么旧的“磁盘”,那么将最大并发操作数设置为磁盘的独立磁头数是有意义的。 ;) ;)

Well, this all may make only a difference, when the connection is really fast and if you are able to process that much data in the same time. 好吧,当连接速度非常快并且您能够同时处理那么多数据时,这一切都只会有所作为。 We are talking about 5 Mbyte per second on a device, and probably 25 Mbyte per second on a lab top. 我们说的是设备上的每秒5 MB,实验室顶部可能是每秒25 MB。

I would recommend using AFNetworking ( AFNetworking on Github ) which has built-in functionality for queuing operations. 我建议使用AFNetworking( Github上的AFNetworking ),它具有用于排队操作的内置功能。 If you only use it to load images that need to be displayed in a table view cell, you could use the AFNetworking category on UIImageView to load these images asynchronously. 如果仅使用它来加载需要在表视图单元格中显示的图像,则可以使用UIImageView上的AFNetworking类别来异步加载这些图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM