检查哪个请求是来自NSURLConnection委托的 [英] check which request is which from NSURLConnection delegate

查看:37
本文介绍了检查哪个请求是来自NSURLConnection委托的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查哪个请求的最佳方法是委托方法中的哪个:

What is the best way to check which request is which inside the delegate method:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

}

现在我有一个NSURLConnection,我在发出请求之前将其设置为NSURLConnection,并且在 didReceiveResponse 里面:

Right now I have a NSURLConnection that I set to the NSURLConnection before making a request and inside didReceiveResponse I do:

if (self.tempConnection == connection)

但是,这可能不适用于比赛条件.有更好的方法吗?

however there is a possiblity this won't work for race conditions. Is there a better way to do this?

推荐答案

在OS5中有更好的方法.忘记所有那些麻烦的委托消息.让连接为您构建数据,然后将完成的代码与起始代码保持一致:

There is a better way in OS5. Forget about all those bothersome delegate messages. Let the connection build the data for you, and put your finished code right in line with your start code:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.site.com"]];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
    NSLog(@"got response %d, data = %@, error = %@", [httpResponse statusCode], data, error);
}];

这篇关于检查哪个请求是来自NSURLConnection委托的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆