当有多个 NSURLConnection 时,如何识别哪个 NSURLConnection 完成加载? [英] How to identify WHICH NSURLConnection did finish loading when there are multiple ones?
问题描述
多个 NSURLConnections 正在启动(在单个 UIViewController 中)以收集不同类型的数据.当他们返回 (-connectionDidFinishLoading) 时,我想根据到达的数据类型处理数据.但是有一个问题,我怎么知道 NSURLConnection 返回了哪个?我需要知道,以便我可以针对出现的数据类型采取行动.(例如,如果是 twitter xml 数据,则显示 twitter 更新)(例如,如果是照片,则显示图像)
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)
人们通常如何解决这个问题?
How do people usually solve this?
推荐答案
您在委托中保留指向两个连接的指针,并将它们与 connection:didReceiveData:<中的
connection
参数进行比较/code> 和 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 时,如何识别哪个 NSURLConnection 完成加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!