我有一個發送請求的嵌套循環。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"
。
[NSURLConnection sendSynchronousRequest:request returningResponse:Response error:nil] –
您可以使用帶有addDependency或MaxConcurrentOperation的NSOperationQueue。 –
@PKT我認爲你的解決方案對我來說已經足夠了。謝謝 – user1151874