【问题标题】:download one by one using nsurlconnection使用 nsurlconnection 逐个下载
【发布时间】:2013-10-27 10:01:44
【问题描述】:

我想使用 NSURLconnection 一个一个地下载多个文件,一个可以做的最大下载是 5。这个过程就像启动第一次下载,当第一次下载完成时才开始第二次下载,当第二次完成时开始第三个。第四次下载和第五次下载也是如此。我可以下载一个文件。相同的代码是:

- (IBAction)download1:(id)sender {
    _fileName = @"dog-wallpaper-dogs.jpg";
    _currentURL = [NSString stringWithFormat:@"http://noruffdaysdotcom1.files.wordpress.com/2012/04/%@",_fileName];
    NSLog(@"currenturl%@",_currentURL);

    NSLog(@"the filename is %@",_fileName);

    NSURL *url =[NSURL URLWithString:_currentURL];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

    _receivedData = [[NSMutableData alloc]initWithLength:0];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self startImmediately:YES];


}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSLog(@"THE STATUS CODE IS %d",[httpResponse statusCode]);
    statuscode = [httpResponse statusCode];
    NSLog(@"into didReceiveResponse");
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [_receivedData setLength:0];
    expectedBytes = [response expectedContentLength];
    NSLog(@"EXPECTED BYTES:%ld",expectedBytes);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
   // NSLog(@"into did receivedata");
    [_receivedData appendData:data];
    //   float progressive = (float)[_receivedData length] / (float)expectedBytes;


}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    flag= 0;
    NSLog(@"into didfailwitherror");
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSLog(@"connection failed");
}

-(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{   flag = 0;
    NSLog(@"into didfinishloading");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"DOCUMENT DIRECTORY :%@",documentsDirectory);
    //   [NSFileManager isWritbleAtPath:documentsDirectory];
    //  [NSFileManager isWritableAtPath:documentsDirectory];
    _imagePath = [documentsDirectory stringByAppendingPathComponent:_fileName];
    NSLog(@"iamge path:%@",_imagePath);
    NSLog(@"succeeded");
    [UIApplication sharedApplication].networkActivityIndicatorVisible= NO;
    NSLog(@"Succeeded! Received %d bytes of data",[_receivedData length]);

    // flag= [_receivedData writeToFile:imagePath atomically:NO];
    if([_receivedData writeToFile:_imagePath atomically:YES])
    {
        flag= 1;
        NSLog(@"write successfull");

    }
    else{
        flag =0;
        NSLog(@"write failed");
    }
    UIImage *img =[UIImage imageWithContentsOfFile:_imagePath];
    self.imageview.image = img;
    isloaded = YES;
}

请帮助我在之前完成后如何进行下一次下载。

【问题讨论】:

    标签: ios download queue nsurlconnection


    【解决方案1】:

    我建议你这样使用NSURLConnection:

    创建一个 maxConcurrentOperationCount = 1 的操作队列

    NSOperationQueue *requestsQueue = [[NSOperationQueue alloc] init];
    [_requestsQueue setMaxConcurrentOperationCount:1];
    

    并以这种方式开始所有连接:

     NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:@"http://yourUrlRequest.com"];
     .
     . // configure your request
     .
    
     [NSURLConnection sendAsynchronousRequest:urlRequest
                                       queue:requestsQueue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    
    // handle your request here
    }
    

    当请求结束时下一个开始,所以添加你所有的异步请求,它们将被一个一个启动(队列并发=1)

    如果一个失败并且您想取消所有请求,只需暂停队列并删除所有请求

    [requestsQueue setSuspended:YES];
    [requestsQueue cancelAllOperations];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多