質問

このコードを使用して配列をループして複数のファイルをダウンロードし、ディスクに書き込みます。

-(void)download
{
//set url paths
for (NSString *filename in syncArray)
{
    NSString *urlpath = [NSString stringWithFormat:@"http://foo.bar/photos/%@", filename];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlpath]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:filename];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];


[operation start];

しかし、問題は、各ファイルが完了した後に成功ブロックを呼び出すことです(これが必要です)が、いくつかのデータをリロードして進行状況を終了するために最後のコールバックが必要です。

正しい方向のポインターは素晴らしいでしょう。

役に立ちましたか?

解決

いつかこれは誰かを助けるかもしれませんが、おそらく大きな問題を抱えているが、私の簡単な使用には大丈夫な回避策を使用することができました。

同期配列が処理された後、各行を同期配列から削除し、必要なコードを実行しました。

 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Successfully downloaded file to %@", path);
    [SVProgressHUD showWithStatus:@"Updating Photos"];
    [syncArray removeObject:filename];
    if (!syncArray || !syncArray.count) 
    {
    NSLog(@"array empty");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self];
        [SVProgressHUD dismissWithSuccess:@"Photos Updated"];
    }

他のヒント

afhttpclientを使用できます EnqueueBatchoperations そして、これには、すべての操作が完了したときに呼び出される完了ブロックがあります。まさにあなたが探しているものであるべきです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top