Creating multiple NSURLConnections. How to identify which Async call

后端 未结 5 1696
借酒劲吻你
借酒劲吻你 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:23

    I prefer different delegates for each connection, too. Although it's a bit of overhead. Fortunately, you can simplify things by using blocks. It's a new feature that doesn't exist in standard SDK yet, but there is 3rd-party framework called PLBlocks that you can use already. Here is an article on how to use them, it also contains example for NSURLConnection.

    This is the client code making HTTP request with block callback:

    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
    [NSURLConnection sendAsynchronousRequest:req onCompletionDo: ^(NSData *data, NSURLResponse *res, NSError *err) {
        NSLog(@"data: %ld bytes. res: %@, error: %@", (long)[data length], res, err);
        [cell.activity stopAnimating];
    }];
    
    0 讨论(0)
  • 2021-02-06 02:24

    What I do in my projects is create a wrapper class for the connection. This way, you can keep a new instance for each connection you need, and maintain these classes in another manager class.

    Something like [AsynchronousConnection initWithURL:delegate:selector:]

    Then you can be ensure the right thing is called when the NSURLConnection is done/failed.

    0 讨论(0)
  • 2021-02-06 02:34

    I used to create a custom wrapper around NSURLConnection, too, but I've now switched over to ASIHTTPRequest. This is a fantastic library providing much more flexibility and features than NSURLConnection. Have a look and give it a try, it's really worth it.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-06 02:41

    Please do not refer https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

    GO to the NSURLConnection.h file and you will find the following.

    When created, an NSURLConnection performs a deep-copy of the NSURLRequest. This copy is available through the -originalRequest method. As the connection performs the load, this request may change as a result of protocol canonicalization or due to following redirects. -currentRequest can be used to retrieve this value.

    Ultimately [connection currentRequest].URL absoluteURL might help.

    Regards, PRASANNA.

    0 讨论(0)
提交回复
热议问题