5

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

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

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

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

4

2 に答える 2

5

いつかこれは誰かを助けるかもしれませんが、おそらく大きな問題がある回避策を使用することができましたが、私の単純な使用法では問題ありません。

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

 [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"];
    }
于 2011-12-07T18:55:03.733 に答える
5

AFHTTPClientを使用してenqueueBatchOperationsを実行できます。これには、すべての操作が終了したときに呼び出されるcompletionBlockがあります。まさにあなたが探しているものでなければなりません。

于 2013-01-31T16:35:21.377 に答える