In everyday applications, we often use third-party libraries such as afnetworking to implement the network request section. This article will briefly explain how to use Nsurlconnection to make asynchronous network requests.
Let's start with a little demo.
- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.NSString*urlstr =@"http://www.baidu.com"; Nsurlrequest*request =[Nsurlrequest Requestwithurl:[nsurl urlwithstring:urlstr]; //from this experiment, it can be seen that the function of connection is not called at all in Mainrunloop running for loop, thus, the connection here is run in Mainthread. Nsurlconnection *con = [[Nsurlconnection alloc] Initwithrequest:requestDelegate: Self]; for(inti =0;i<10000; i++) {NSLog (@"%d", i); } NSLog (@"For end==========");}- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{NSLog (@"Good!!!!!!!!");}
Look at the input.
the- to- to Ten: the:18.861urlconnectionasynctest[9988: 60b]9997 the- to- to Ten: the:18.862urlconnectionasynctest[9988: 60b]9998 the- to- to Ten: the:18.862urlconnectionasynctest[9988: 60b]9999 the- to- to Ten: the:18.862urlconnectionasynctest[9988: 60b] forend========== the- to- to Ten: the:18.865urlconnectionasynctest[9988: 60b] Good!!!!!!!!
Looking at Apple's documentation will find the following explanations
by default , a connection is scheduled on the current thread in the default Mode when it delegate : Startimmediately:method and provide NO for The startimmediately parameter, you can instead schedule the connection on a operation queue before starting it with th E Start method. You cannot reschedule a connection after it has started.
This means that if you create a connection object by using the Initwithrequest:delegate: method in the main thread only, it is added to Mainthread by default, so that when the data is returned it is executed in the main thread, This can affect the refresh of the UI. This connnection is not a synchronous connection because it can continue executing other code without getting the data, but it is not really an asynchronous connection because its callback function is executed in the main thread, which affects the main Other functions in thread are executed.
So how do you create a connection that executes a callback function in another thread? The system provides 2 sets of methods,
The first set is to use the class method, SendAsynchronousRequest:queue:completionHandler:
The second set uses several object methods, in the following order
1. Use initwithrequest:delegate: startimmediately: Generate a connnection 2 that does not start immediately. Using the Scheduleinrunloop:formode: or setdelegatequeue: Set the thread for the callback method to run, the second is recommended. 3. Call start to start the connection request.
A demo is given below
- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.NSString*urlstr =@"http://www.baidu.com"; Nsurlrequest*request =[Nsurlrequest Requestwithurl:[nsurl urlwithstring:urlstr]; //from this experiment, it can be seen that the function of connection is not called at all in Mainrunloop running for loop, thus, the connection here is run in Mainthread. //nsurlconnection *con = [[Nsurlconnection alloc] initwithrequest:request delegate:self];nsurlconnection*con = [[Nsurlconnection alloc] Initwithrequest:requestDelegate: Self startimmediately:no]; [Con setdelegatequeue:[[nsoperationqueue alloc] init]; [Con start]; for(inti =0;i<10000; i++) {NSLog (@"%d", i); } NSLog (@"For end==========");}- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{//NSLog (@ "Data is%@", [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]);NSLog (@"Good!!!!!!!!");}
The results are as follows
the- to- to One: at:15.129urlconnectionasynctest[10147: 60b]344 the- to- to One: at:15.129urlconnectionasynctest[10147: 60b]345 the- to- to One: at:15.129urlconnectionasynctest[10147: 60b]346 the- to- to One: at:15.129urlconnectionasynctest[10147: 60b]347 the- to- to One: at:15.129urlconnectionasynctest[10147: 60b]348 the- to- to One: at:15.129urlconnectionasynctest[10147: 60b]349 the- to- to One: at:15.130urlconnectionasynctest[10147: 60b] - the- to- to One: at:15.134urlconnectionasynctest[10147:4207] Good!!!!!!!! the- to- to One: at:15.143urlconnectionasynctest[10147: 60b]351 the- to- to One: at:15.144urlconnectionasynctest[10147: 60b]352 the- to- to One: at:15.144urlconnectionasynctest[10147:4207] Good!!!!!!!! the- to- to One: at:15.144urlconnectionasynctest[10147: 60b]353 the- to- to One: at:15.144urlconnectionasynctest[10147: 60b]354 the- to- to One: at:15.144urlconnectionasynctest[10147: 60b]355 the- to- to One: at:15.145urlconnectionasynctest[10147: 60b]356
It can be seen that connection's callback function is no longer executed in main thread!