iOS-多个NSURLConnection下载 [英] iOS - multiple NSURLConnection downloads

查看:60
本文介绍了iOS-多个NSURLConnection下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 NSURLConnection 下载一些文件:

I'm trying to download some files using NSURLConnection:

- (void)startDownload {
  NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
  [req addValue:@"Basic XXXXXXXXXXXXXX=" forHTTPHeaderField:@"Authorization"];
  connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {
  filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:save_name];
  [[NSFileManager defaultManager] createFileAtPath:filepath contents:nil attributes:nil];
  file = [[NSFileHandle fileHandleForUpdatingAtPath:filepath] retain];
  [file seekToEndOfFile];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  [file seekToEndOfFile];
  [file writeData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
  [file closeFile];
 }

我的文件很大,所以我必须保存我下载的每个文件磁盘,无法将它们存储在RAM中。问题是,我该如何处理多个下载?

My files are quite big, so I have to save every piece I download to a disk, can't store them in RAM. The question is, how can I handle multiple downloads?

我想在下一个下载完成后在-中开始下一个下载(无效)connectionDidFinishLoading:,但我想这并不是最好的方法。有什么建议吗?

I was thinking to start the next download after the previous one is finished in - (void)connectionDidFinishLoading:, but I guess it is not the best way to do it. Any suggestions?

推荐答案

如果您想使用NSURLConnection,我建议您制作一个自己的下载对象,该对象可以包装连接并进行编写

If you want to use NSURLConnection I would recommend making your own download object that wraps the connection and deals with writing it to disk.

您将使用文件名初始化对象以将完成的文件放入其中,并使用URL来获取文件,例如:

You would initialise your object with a filename to put the finished file in and the URL to get the file from, something like :

MyDownloadObject *o = [[MyDownloadObject alloc] initWithFilename:filename URL:url];
o.delegate = self;

每个下载对象都将处理自己的NSURLRequest和NSURLConnection,使用与您的代码几乎相同的代码问题:)

Each download object would deal with it's own NSURLRequest and NSURLConnection, using pretty much the same code as in your question :)

完成后,它会告诉您的视图控制器它已下载(可能通过委托方法,但通知也可以)。然后,您可以从委托方法中得知哪个文件已完成。

When it's finished it would tell your view controller that it's downloaded (probably via a delegate method but a notification would work just as well). You can then tell from the delegate method which file has finished.

- (void)myDownloadObjectFinishedDownload:(MyDownloadObject *)o {
    NSLog(@"File from %@ has been stored at %@", o.URL, o.filename);
}

如果将MyDownloadObject设置为NSOperation的子类,则可以使用NSOperationQueue来限制并发下载并监视总体进度等。

If you made your MyDownloadObject a subclass of NSOperation then you could use an NSOperationQueue to limit the concurrent downloads and monitor overall progress etc.

如果您不介意使用第三方库,则ASIHTTPRequest是一个不错的选择,但是要警告,它不再被开发了。我怀疑堆栈溢出的其他人会推荐更好,最新的库,您可以改用这些库。

If you didn't mind using a third party library then ASIHTTPRequest is a good choice but be warned, it's not being developed anymore. I suspect that other people on stack overflow will recommend better, more up to date, libraries you could use instead.

这篇关于iOS-多个NSURLConnection下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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