2013-10-27 2 views
0

NSURLconnection을 사용하여 하나씩 여러 파일을 다운로드하려고합니다. 최대 다운로드 수는 5입니다. 프로세스는 첫 번째 다운로드를 시작한 것과 같습니다. 첫 번째 다운로드가 완료되면 두 번째 다운로드 만 시작됩니다. 앞으로 다운로드 다섯 번째 download.i이 같은 단일 파일을 국지적 인 코드를 다운로드 할 수 있어요에 대한 두 번째가 완료 세 번째 one.Likewise를 시작할 때와 같습니다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]; 
관련 문제