Creating multiple NSURLConnections. How to identify which Async call

后端 未结 5 1699
借酒劲吻你
借酒劲吻你 2021-02-06 02:05

I am intending to create 2 requests using NSURLConnection. When the server responds and calls connectionDidFinishLoading it passes in the connection as the parameter, but how d

5条回答
  •  死守一世寂寞
    2021-02-06 02:36

    Save both NSURLConnection objects as member variables of whatever delegate object you passed to connectionWithRequest:delegate:. Then you can just compare each of those to the NSURLConnection passed to connectionDidFinishLoading:, and respond appropriately:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        if (connection == firstConnection) {
            // do something
        }
        else if (connection == secondConnection) {
            // do something else
        }
    }
    

    Another slightly more object-oriented option would be to create two different delegate objects, each of which knows how to deal with each each type of connection. Then just pass the appropriate delegate when you create each connection. That way you don't need to check to see which connection you have, because each delegate will only receive connectionDidFinishLoading: for its own connection.

提交回复
热议问题