了解GCD块内[NSData dataWithContentsOfURL:URL]的行为 [英] Understanding the behaviour of [NSData dataWithContentsOfURL:URL] inside the GCD block

查看:161
本文介绍了了解GCD块内[NSData dataWithContentsOfURL:URL]的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题/陈述仅限于我的理解,请告诉我这里是不是我错了。



来自: GCD问题和线程太多



转发到jackslash的答案,我想大多数开发人员都习惯使用 [NSData dataWithContentsOfURL:URL] 这肯定会阻塞(直到它的如果你通过URL获取一些小细节,这可能是理想的。但是在多个进程的情况下(我的意思是在下载多个文件时),GCD必须创建许多线程并且不是下载数据的完美方式。 / p>

因此一些专家建议使用名为 [NSURLConnection sendAsynchronousRequest:queue:completionHandler:的GCD抽象来处理这种情况(和也很理想。)



我唯一的问题是 [NSURLConnection sendAsynchronousRequest:queue:com pletionHandler: 将优先于 NSData dataWithContentsOfURL:URL] 在创建新线程和避免获取方面阻止?。



真的很感激您的任何记录答案。

解决方案

你的问题不是关于GCD:它是关于同时下载许多文件的最佳策略。



最好的策略只是不做它。试图同时下载100个单独的文件是一个坏主意,特别是如果用户是在蜂窝连接上(他们可能是)。你是对的,确定 dataWithContentsOfURL 通常也不是一个好主意。



这就是说,这里有一个中间立场。如果有一种方法可以告诉iOS一次下载X个文件,但不超过那个,那就太好了。为此,我们可以使用 NSOperationQueue 。操作队列包含许多操作并将它们放入队列(在本例中是在后台运行)。例如:

  NSOperationQueue * operationQueue = [[NSOperationQueue alloc] init]; 
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@ [download1,download2,download3] waitUntilFinished:NO];

这里我们创建了一个可以支持三个同时下载的操作队列。使用操作队列的优点应该是显而易见的。许多开发人员选择使用像 AFNetworking 这样的库来帮助他们管理下载,并且有很多指南可以使用这个和 NSURLConnection 在一个操作队列中。


Following question/statement is limited to my understanding,let me know if i'm wrong here.

From : Issue with GCD and too many threads

Forward to jackslash's answer,i wonder most of the developers have a habbit of using [NSData dataWithContentsOfURL:URL] which certainly does seems to be blocking(until it's done).Which can be ideal if your fetching some small details through URL.But in case of multiple process(i mean while downloading multiple files),GCD has to create many many threads and is not a perfect way to download the data.

So some experts suggested to use GCDs abstraction called [NSURLConnection sendAsynchronousRequest:queue:completionHandler: to handle such situation(and is ideal also).

My only question is how does [NSURLConnection sendAsynchronousRequest:queue:completionHandler: will take the advantage over NSData dataWithContentsOfURL:URL] in terms of creating new threads and saving us from getting blocked?.

Really appreciate any of your documented answer.

解决方案

Your question isn't really about GCD: it's about the best strategy to download many files at the same time.

The best strategy simply is to not do it. Trying to download, say, 100 individual files at the same time is a bad idea, especially if the user is on a cellular connection (which they probably are). You're right to identify that dataWithContentsOfURL is also usually not a great idea.

That said, there's a middle ground here. It would be nice if there was a way to tell iOS "go download X number of files at a time, but no more than that". To do this we can use a NSOperationQueue. An operation queue contains a number of operations and places them into a queue (that in this case is running in the background). For example:

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[download1, download2, download3] waitUntilFinished:NO];

Here we've created an operation queue that can support three simultaneous downloads. The advantages of using an operation queue should be obvious. Many developers choose to use a library like AFNetworking to help them manage downloading, and there are lots of guides around to using both this and NSURLConnection in an operation queue.

这篇关于了解GCD块内[NSData dataWithContentsOfURL:URL]的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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