How to determine WHICH NSURLConnection has finished loading when there are several?

Several NSURLConnections are started (in one UIViewController) to collect different kinds of data. When they return (-connectionDidFinishLoading), I want to do data with the data, depending on the type of data that came. But one problem, HOW DO I KNOW THAT NSURLConnection is back? I need to know, so I can take specific actions by data type. (For example, displaying a twitter update if it was twitter xml data) (for example, displaying an image if it was a photo)

How do people usually solve this?

+4
source share
1 answer

You store pointers to both connections in the delegate and compare them with the connection parameter in connection:didReceiveData: and connectiondidFinishLoading:

For instance:

 @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 } } 
+13
source

Source: https://habr.com/ru/post/1304554/


All Articles