Creating multiple NSURLConnections. How to identify which Async call

后端 未结 5 1693
借酒劲吻你
借酒劲吻你 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];
    }];
    

提交回复
热议问题