Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

iphone - How to identify WHICH NSURLConnection did finish loading when there are multiple ones?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You keep pointers to both connections in the delegate, and compare these to the connection parameter in connection:didReceiveData: and connectiondidFinishLoading:

For example:

@interface Foo : NSObject {
    NSURLConnection *connection1;
    NSURLConnection *connection2;
}

and

connection1 = [NSURLConnection connectionWithRequest:request1 delegate:self];
connection2 = [NSURLConnection connectionWithRequest:request2 delegate:self];

and

- (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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...