如何识别多个NSURLConnection完成加载? [英] How to identify WHICH NSURLConnection did finish loading when there are multiple ones?
问题描述
启动多个NSURLConnections(在单个UIViewController中)以收集不同类型的数据。当他们返回(-connectionDidFinishLoading)时,我想对数据进行处理,具体取决于已到达的数据类型。但有一个问题,我怎么知道NSURLConnection返回了什么?我需要知道,所以我可以针对所提供的数据类型采取行动。 (例如,如果是推特xml数据,则显示推特更新)(例如,如果是照片则显示图像)
Multiple NSURLConnections being started (in a single UIViewController) to gather different kinds of data. When they return (-connectionDidFinishLoading) I wanna do stuff with the data, depending on the type of data that has arrived. But one prob, HOW DO I KNOW WHICH NSURLConnection returned? I need to know so I can take action specific to the type of data that came. (Eg. display a twitter update if it was the twitter xml data)(Eg. display an image if it was a photo)
人们通常如何解决这个问题? / p>
How do people usually solve this?
推荐答案
您保留指向委托中两个连接的指针,并将它们与连接$ c进行比较$ c>参数
连接:didReceiveData:
和 connectiondidFinishLoading:
You keep pointers to both connections in the delegate, and compare these to the connection
parameter in connection:didReceiveData:
and connectiondidFinishLoading:
例如:
@interface Foo : NSObject {
NSURLConnection *connection1;
NSURLConnection *connection2;
}
和
connection1 = [NSURLConnection connectionWithRequest:request1 delegate:self];
connection2 = [NSURLConnection connectionWithRequest:request2 delegate:self];
和
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if(connection == connection1) {
// Connection 1
} else if(connection == connection2) {
// Connection 2
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if(connection == connection1) {
// Connection 1
} else if(connection == connection2) {
// Connection 2
}
}
这篇关于如何识别多个NSURLConnection完成加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!