Does all NSURLConnections connect asynchronously?

2019-08-17 03:12发布

问题:

I've looked through some NSURLConnection examples, and all are titled as related asynchronously, although I didn't see any operation queue / thread/ dispatch created. I was wondeirng - does NSURLConnection always preform requests asynchronously or you have to set something special for it?

回答1:

NSURLConnection can be run in synchronous or asynchronous mode depending on what method you call to make the actual connection once you've allocated the NSURLConnection Instance (or use the Class Methods).

For synchronous Connections, you use the method:

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error

For asynchronous connections, you use the methods:

+ (NSURLConnection *)connectionWithRequest:(NSURLRequest *)request delegate:(id < NSURLConnectionDelegate >)delegate

- (id)initWithRequest:(NSURLRequest *)request delegate:(id < NSURLConnectionDelegate >)delegate

- (id)initWithRequest:(NSURLRequest *)request delegate:(id < NSURLConnectionDelegate >)delegate startImmediately:(BOOL)startImmediately

- (void)start

I recommend reading Apple's Documentation on NSURLConnection to find out more information and looking at sample code