2013-10-27 28 views
0

我想用NSURLconnection一個接一個地下載多個文件,而且可以做的最大下載量是5.這個過程就像初次下載,當第一次下載完成後纔開始第二次下載當第二個是完整的啓動第三one.Likewise的第四下載和第五download.i我能夠下載一個文件.The代碼相同的是:使用nsurlconnection一個接一個下載

- (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; 
} 

請幫助我,我怎麼就下一步下載早些時候完成。

回答

0

我建議你以這種方式使用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];