2016-04-27 6 views
0

リクエストを送信するネストループがあります。NSURLConnectionはすべてのプロセスを完了した後にリクエストを送信します

-(void) download 
{ 
    for(NSString *id in array) 
    { 
    //init with request and start the connection 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request deletegate:self]; 
    [conn start]; 
    } 
} 

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 
{ 
//enter here secondly 
} 
-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{ 
//enter here last, after finish the for loop 
//my intention is use the downloaded data to do something before sending a new request. 
} 

問題は、私は前にのためのループで再びリクエストを送る最初の"-(void) connectionDidFinishLoading:(NSURLConnection *) connection"を入力したいということです。

しかし、現在はforループを終了し、すべての要求を"-(void) connectionDidFinishLoading:(NSURLConnection *) connection"に入力する前に送信します。

+1

[NSURLConnection sendSynchronousRequest:request returningResponse:レスポンスエラー:なし] –

+0

addDependencyまたはMaxConcurrentOperationを指定してNSOperationQueueを使用することができます(この場合、一般的なアドバイスも適用されます)。 –

+0

@PKTあなたの解決策は私にとっては十分だと思います。ありがとう – user1151874

答えて

1

あなたはNSURLConnectionががiOS9

for (NSString *URL in URLArray) { 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; 

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
    // check error and/or handle response here 
}]; 
[task resume]; 
} 

を非推奨となり、ループdispatch_group_enter(group);ためにdispatch_group_t group = dispatch_group_create();

追加ラインを使用して、あなたの目標

ため

dispatch_group_notify(group, dispatch_get_main_queue(), ^{ 
    // Request Finish 
}); 

を呼び出します。このを試してみてください

0

お客様の要件に応じて、別のリクエストに対する最初の接続の応答を必要とするため、ブロック機能を試す必要があります。

for(NSString* url in array) 
{ 
    // Generate a NSURLRequest object from the address of the API. 
    NSURL *url = [NSURL URLWithString:urlLink]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    // Send the request asynchronous request using block! 
    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

          if (error) { 
           NSLog(@"Error in updateInfoFromServer: %@ %@", error, [error localizedDescription]); 
          } else if (!response) { 
           NSLog(@"Could not reach server!"); 
          } else if (!data) { 
           NSLog(@"Server did not return any data!"); 
          } else { 
           [self doStuffWithData:data]; 
          } 
         }]; 
} 
0

URLの読み込みは同期動作ではありません(あるいは、少なくともは、同期的に行わすべきではありません)それだけでDNSルックアップの失敗のために90秒ほどかかることがありますので、ほぼ無限に長いサーバーが出てドリブルし続ける場合データ。その時間のほんの一部でもメインスレッドをブロックすると、iOSによってアプリが強制終了されます。

ループ内の要求をスケジュールして終了するのではなく、最初の要求(および最初の要求のみ)をスケジュールする必要があります。その後、connectionDidFinishLoading:メソッド(とconnection:DidFailWithError:メソッド)で、次のリクエストをスケジュールします。

iOS 6/10.8以前をサポートする必要がある場合を除き、おそらくNSURLSessionを使用しているはずです。

関連する問題