下载后台线程中的多个文件?
问题描述:
我一次只能根据用户需求下载文件,或者一个一个或一个下载文件。下载文件后,我将通知发送到另一个视图作为sucessfull消息。下载后台线程中的多个文件?
当我一次下载单个文件时,它正在成功下载文件。但是当我试图在6秒的时间间隔内下载两个或多个文件(按下另一个下载按钮)时,第一个文件不会被下载。它仅下载我发送下载的最后一个文件。
任何帮助,将不胜感激。
url=[NSURL URLWithString:currentURL];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setHTTPMethod:@"GET"];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{ //Background Thread
{
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *dataMain, NSError *error)
{
if ([dataMain length]/1024.0f > 600 && error == nil)
{
[dataMain writeToFile:pathOriginal atomically:YES];
NSLog(@"orginal file saved");
}
}];
}
dispatch_async(dispatch_get_main_queue(), ^(void){
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self];
}); });
答
在每次调用之前使用调度异步。这样每个电话都会在不同的线程上运行,并解决您的问题。
希望这会有所帮助!
答
NSURLConnection
已弃用。您应该使用NSURLSession
进行新的开发。 NSURLSession
将处理多个下载。 (所以才会NSURLConnection
,但它不值得调试,鉴于它的弃用。
u能细说吗? – Abhiram
林移动不能发布代码片段,但复制此dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^(无效){// 后台线程 dispatch_async(dispatch_get_main_queue(),^(无效){// 运行UI更新 }); }); –
i相 – Abhiram