如何识别多个NSURLConnection在多个时完成加载?

时间:2010-03-19 09:58:39

标签: iphone xcode uiviewcontroller nsurlconnection

启动多个NSURLConnections(在单个UIViewController中)以收集不同类型的数据。当他们返回(-connectionDidFinishLoading)时,我想对数据进行处理,具体取决于已到达的数据类型。但有一个问题,我怎么知道NSURLConnection返回了什么?我需要知道,所以我可以采取特定的数据类型的行动。 (例如,如果是twitter xml数据,则显示Twitter更新)(例如,如果是照片则显示图像)

人们通常如何解决这个问题?

1 个答案:

答案 0 :(得分:13)

您在代理中保留指向两个连接的指针,并将这些连接与connectionconnection:didReceiveData:中的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
    }
}