2

非同期操作リクエストを使用しようとしていますが、ある時点でリクエスト タイムアウトにより操作リクエストが失敗しました。タイムアウト操作が再送信され、すべての操作が失敗または完了したがタイムアウトなしで完了したときに操作を実行できるように、ブロックを形成するにはどうすればよいですか。

私は本当にこれを理解する必要があります、どうもありがとう!

[[SDAFParseAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:pagedOperations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
                    NSLog(@"PAGED totalNumberOfOperations: %u  numberOfCompletedOperations: %u",totalNumberOfOperations,numberOfCompletedOperations);
                } completionBlock:^(NSArray *operations) {

                    NSMutableArray *retryops = [NSMutableArray array];

                    for(AFHTTPRequestOperation *operation in operations){
                        if(operation.error.code == -1001){
                            NSLog(@"error code %d",operation.error.code);

                            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                                NSLog(@"comp");
//actually original completion block is needed
                            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                NSLog(@"fail");
  //actually original fail block is needed
                            }];
                            [retryops addObject:operation];
                            //Do something with the response
                        }else{
                            //Handle the failure
                        }
                    }

                    if ([retryops count] == 0) {
                        NSLog(@"all paginated operations completed");
                    }
                    else {
                        [[SDAFParseAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:retryops progressBlock:
                         ^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

                         } completionBlock:
                         ^(NSArray *operations) {
                             NSLog(@"all retry paginated operations completed");
                         }];
                    }
4

1 に答える 1

3

私は同じ問題を抱えていました。FTP サイトから 200 以上のファイルをダウンロードしようとしていましたが、enqueueBatchOfHTTPRequestOperations に渡された NSURLRequests は、100 番目のファイルがダウンロードされた後に常にタイムアウトしていました。AFNetworking は不要であり、ソリューションが複雑だったため、最終的には完全に削除しました。これは、部分的に他のいくつかのSO回答の混合です。

NSArray *requestsArray = // my array of [[NSMutableURLRequest alloc] initWithURL:url]'s
dispatch_queue_t downloadQueue = dispatch_queue_create("downloadQueue", NULL);
dispatch_async(downloadQueue, ^{
    NSURLResponse *response;
    NSError *requestError;
    for (NSURLRequest *request in requestsArray) {
        response = nil;
        requestError = nil;
        NSData *requestData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
        if (response == nil) {
            // Check for problems
            if (requestError != nil) {

            }
        }
        else {
            // Data was received.. continue processing
            // Make sure to do any GUI related updates in main queue/thread,
            // not directly from here
        }
    }
});

これは、すべての同期 NSURLRequest が一度に 1 つずつ実行されるように、シリアル ディスパッチ キューを使用します。各リクエストが完了すると、すべてのリクエストが完了するのを待つのではなく、リクエストからのデータを処理します。これはメイン キューにないため、GUI をブロックしません。

于 2013-03-18T19:49:04.677 に答える