Does all NSURLConnections connect asynchronously?

2019-08-17 03:07发布

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条回答
我想做一个坏孩纸
2楼-- · 2019-08-17 03:36

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

查看更多
登录 后发表回答