我有两个请求一个接一个地开始。像这样的起始请求
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
NSURLConnection * connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[connection start];
和另一个这样的请求。
NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.apple.com"]];
NSURLRequest *request1 = [NSURLRequest requestWithURL:url1 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
NSURLConnection *connection1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
[connection1 release];
如何在委托方法中区分这两者?
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{}
不想为此目的保留任何额外的类变量。
答案 0 :(得分:4)
很简单:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection == connection1)
{
//It's for connection1.
}
else if (connection == connection2)
{
//It's for connection2.
}
}
你可以通过这个美丽的SO问题:Managing multiple asynchronous NSURLConnection connections
答案 1 :(得分:2)
将你的NSURLConnection
个对象放在.h文件中,并按照Markus的建议检查你的委托方法。
或
对您的NSURLConnection
进行子类化,然后您可以在创建时将tag
属性添加到连接类,在委派方法中检查适当的标记。您可以找到working tutorial here。