#import "ViewController.h"@interfaceViewcontroller () <NSURLConnectionDataDelegate>@end@implementationViewcontroller-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{[self newThreadDelegate2];}-(void) delegate1{nsurlrequest*request = [Nsurlrequest requestwithurl:[nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]]; //Set up proxy//Proxy Method: The default is called in the main threadNsurlconnection *connect = [Nsurlconnection connectionwithrequest:requestDelegate: Self]; //sets the agent method to invoke in which thread//[Nsoperationqueue Alloc]init]] Open sub-thread//[Nsoperationqueue Mainqueue] cannot be set like this[Connect Setdelegatequeue:[[nsoperationqueue alloc]init]; //[Connect Setdelegatequeue:[nsoperationqueue mainqueue];NSLog (@"-------");}-(void) delegate2{nsurlrequest*request = [Nsurlrequest requestwithurl:[nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]]; //Set up proxy//Proxy Method: The default is called in the main threadNsurlconnection *connect = [[Nsurlconnection alloc]initwithrequest:requestDelegate: Self startimmediately:no]; [Connect Setdelegatequeue:[[nsoperationqueue Alloc]init]; //Start Sending Request[Connect start]; NSLog (@"-------");}-(void) newthreaddelegate1{Dispatch_async (Dispatch_get_global_queue (0,0), ^{nsurlrequest*request = [Nsurlrequest requestwithurl:[nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]]; //Set up proxy//Proxy Method: The default is called in the main thread//the method internally adds the Connect object as a source to the current Runloop, specifying the run mode as the defaultNsurlconnection *connect = [Nsurlconnection connectionwithrequest:requestDelegate: Self]; //sets the agent method to invoke in which thread[Connect Setdelegatequeue:[[nsoperationqueue alloc]init]; //[[Nsrunloop Currentrunloop] Runmode:uitrackingrunloopmode beforedate:[nsdate datewithtimeintervalsincenow:1000 ]];[[Nsrunloop Currentrunloop]run]; NSLog (@"---%@----", [Nsthread CurrentThread]); }); }-(void) newthreaddelegate2{Dispatch_async (Dispatch_get_global_queue (0,0), ^{nsurlrequest*request = [Nsurlrequest requestwithurl:[nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]]; //Set up proxy//Proxy Method: The default is called in the main threadNsurlconnection *connect = [[Nsurlconnection alloc]initwithrequest:requestDelegate: Self startimmediately:no]; [Connect Setdelegatequeue:[[nsoperationqueue Alloc]init]; //Start Sending Request//If the Connect object is not added to Runloop, the method is automatically added to the Runloop//Note: If the current runloop is not turned on, the method internally automatically obtains the Runloop object corresponding to the current thread and opens[Connect start]; NSLog (@"---%@----", [Nsthread CurrentThread]); });}#pragmaMark----------------------#pragmaMark Nsurlconnectiondatadelegate-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{NSLog (@"didreceiveresponse---%@", [Nsthread CurrentThread]);}@end
# # # # #1 Nsurlconnection and Runloop (interview)
(1) The difference between the two types of proxy mode for nsurlconnection
"' OBJC
The first way to set up:
The proxy is set by this method, and the request is sent automatically
[[Nsurlconnection alloc]initwithrequest:request delegate:self];
The second way to set up:
When the agent is set to Startimmediately No, the method does not automatically send the request
Nsurlconnection *connect = [[Nsurlconnection alloc]initwithrequest:request delegate:self StartImmediately:NO];
To send a request manually by way of code
Note that the method internally automatically adds connect to the current thread's runloop in the default mode
[Connect start];
```
(2) How to control which thread the proxy method calls
"' OBJC
Description: By default, the proxy method is called in the main thread (in order to make it easier for developers to handle some of the refresh UI without having to take into account inter-thread communication)
To set the execution queue for an agent method
[Connect Setdelegatequeue:[[nsoperationqueue Alloc]init];
```
(3) The attention point of the thread sending network request, suitable for automatic send network request mode
"' OBJC
Send a network request in a child thread-call the Startf method to send
-(void) CreateNewThreadSendConnect1
{
1. Create a non-home column
Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];
2. Encapsulate the operation and add the task to the queue to execute
[Queue addoperationwithblock:^{
NSLog (@ "%@", [Nsthread CurrentThread]);
2-1. Determine the request path
Nsurl *url = [Nsurl urlwithstring:@ "Http://120.25.226.186:32812/login?username=dd&pwd=ww&type=JSON"];
2-2. Create a Request object
Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
2-3. Use Nsurlconnection to set up a proxy to send network requests
Nsurlconnection *connection = [[Nsurlconnection alloc]initwithrequest:request delegate:self StartImmediately:YES];
2-4. Set which queue the proxy method executes in, and if it is a non-home column, then the proxy method executes in the child thread
[Connection Setdelegatequeue:[[nsoperationqueue Alloc]init];
2-5. Send a network request
Note: The Start method internally adds the current Connect object as a source to the corresponding runloop of the current thread
The difference is that if the Start method is called to develop a send network request, then the process of adding the source, if the current runloop does not exist
The method internally creates a runloop that corresponds to the current thread and starts.
[Connection start];
}];
}
Send network requests in a child thread-automatically send network requests
-(void) CreateNewThreadSendConnect2
{
NSLog (@ "-----");
1. Create a non-home column
Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];
2. Encapsulate the operation and add the task to the queue to execute
[Queue addoperationwithblock:^{
2-1. Determine the request path
Nsurl *url = [Nsurl urlwithstring:@ "Http://120.25.226.186:32812/login?username=dd&pwd=ww&type=JSON"];
2-2. Create a Request object
Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
2-3. Use Nsurlconnection to set up a proxy to send network requests
Note: This method will add connection to Runloop, but if the current runloop does not exist, it will not be actively created.
Nsurlconnection *connection = [nsurlconnection connectionwithrequest:request delegate:self];
2-4. Set which queue the proxy method executes in, and if it is a non-home column, then the proxy method executes in the child thread
[Connection Setdelegatequeue:[[nsoperationqueue Alloc]init];
2-5 Create the Runloop corresponding to the current thread and turn on
[[Nsrunloop Currentrunloop]run];
}];
}
```
Summary: That is, sending a network request in the queue to set the callback, the callback task by default in the main thread, if set callback queue in the child thread, then destroy the success will not invoke the proxy method, workaround: 1: Call the Start method: The Start method internally adds the current Connect object as a source to the corresponding runloop of the current thread.
If the Start method is called to develop a send network request, then the process of adding source, if the current runloop does not exist then the method will automatically create a current thread corresponding to the Runloop, and start. Each thread must have its own runloop to handle events 2: If you do not call the Start method, you need to create the runloop yourself and call the Run method,
Dispatch_async (dispatch_get_global_queue (0, 0), ^{ Nsurlrequest *request = [Nsurlrequest requestwithurl:[ Nsurl urlwithstring:@ "Http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]; Set proxy //proxy method: The default is called in the main thread //The method will actually add the Connect object as a source to the current Runloop, specifying the run mode as the default nsurlconnection * connect = [nsurlconnection connectionwithrequest:request delegate: self]; Sets the proxy method in which thread calls [connect Setdelegatequeue:[[nsoperationqueue Alloc]init]]; [[Nsrunloop Currentrunloop] Runmode:uitrackingrunloopmode beforedate:[nsdate datewithtimeintervalsincenow:1000]] ; [[Nsrunloop Currentrunloop]run]; NSLog (@ "---%@----", [Nsthread CurrentThread]);}); }
The difference is that: 1: Call Start if the Start method is called to develop a send network request, then the process of adding source, if the current runloop does not exist then the method will automatically create a current thread corresponding Runloop, and start 2: While the following method automatically sends the request, it will also
Adds the Connect object as a source to the current runloop, specifying that the run mode is the default, but if the current child thread's runloop does not exist, it will not be created automatically and needs to be created manually
Nsurlconnection *connect = [nsurlconnection connectionwithrequest:request delegate:self];
iOS Development Network Learning VI: Set up queue requests with Runloop