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

后端 未结 1 470
Happy的楠姐
Happy的楠姐 2020-12-09 23:20

Multiple NSURLConnections being started (in a single UIViewController) to gather different kinds of data. When they return (-connectionDidFinishLoading) I wanna do stuff wit

相关标签:
1条回答
  • 2020-12-10 00:12

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题