2016-06-21 7 views
0

arrayには異なるURLが含まれています。プログレスバーを使って1つのファイルをダウンロードしたいのですが、次のファイルを開始するなどです。afnetworkingを使用して一度に1つのファイルをダウンロードしてください。

ここに私のコードがあります。

-(void) func:(NSArray *) arry 
{ 

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    configuration.timeoutIntervalForRequest = 900; 
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

    NSMutableArray * downloadedUrl = [[NSMutableArray alloc] init]; 
    for (NSString * str in arry) { 
     NSURL *URL = [NSURL URLWithString:str]; 
     NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

     NSURLSessionDownloadTask downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL (NSURL targetPath, NSURLResponse response) { 
      NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES]; 

      return [tmpDirURL URLByAppendingPathComponent:[response suggestedFilename]]; 
     } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) { 

      if(error) 
      { 
       NSLog(@"File Not Dowloaded %@",error); 

      } 

     }]; 
     [downloadTask resume]; 
    } 
} 

プログレスバーを使って一度に1つのファイルをダウンロードしてから、どのようにURLをアレイから削除しますか?

答えて

2

ファイルのグローバルNSMutableArrayを宣言し、以下のような関数で使用してください。

-(void) downloadFile { 

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    configuration.timeoutIntervalForRequest = 900; 
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

    NSURL *URL = [NSURL URLWithString:[self.fileArr firstObject]]; //Here self.fileArr is your global mutable array 
    NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

    NSURLSessionDownloadTask downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL (NSURL targetPath, NSURLResponse response) { 
    NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES]; 

     return [tmpDirURL URLByAppendingPathComponent:[response suggestedFilename]]; 
    } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) { 

     if(error) 
     { 
      NSLog(@"File Not Dowloaded %@",error); 
      [self downloadFile]; 
     } 
     else { 
      [self.fileArr removeObjectAtIndex:0]; 
      if (self.fileArr.count > 0) { 
       [self downloadFile]; 
      } 
     } 
    }]; 
    [downloadTask resume]; 
} 

は今、そのinitialize self.fileArr前に、その呼び出しdownloadFileメソッドの後にこの関数を呼び出します。

希望すると、これが役立ちます。

+0

ファイルのダウンロードに失敗した場合でも、次のダウンロードのためのプロセスを続行し、必要に応じて失敗のケースを処理する必要があります。 –

+0

今すぐOK ......... :) –

+0

進行状況を計算するには? @NiravDoctorwala – salmancs43

1

与える制限は、一度に1つの操作にキュー、そのために

は、あなたがそれらをキューする前に、各操作間の依存関係を追加してみてください。

キューに追加する前にoperation1とoperation2の2つの操作の間に依存関係を追加すると、operation1が完了またはキャンセルされるまで操作2が開始されません。

それが好きです:

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; 

// Make operation2 depend on operation1 

[operation2 addDependency:operation1]; 

[operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO]; 

UPDATE

// Create a http operation 

NSURL *url = [NSURL URLWithString:@"http://yoururl"]; 

NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

    // Print the response body in text 

    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

    NSLog(@"Error: %@", error); 

}]; 

// Add the operation to a queue 
// It will start once added 

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; 
[operationQueue addOperation:operation]; 

は、それはあなたを助けることを願って...!

+0

チュートリアルがある場合は、それを参照してください。私はAFNetworkingを初めて使用しています – salmancs43

関連する問題