I.NSURLSession is used in two ways:
1.
NSURLSession* session = [NSURLSession sharedSession]
This way, there is no settingNSURLSession delegate, and therefore will not go through the proxy interface, so to achieve meaningful functionality, you need to use a proxy with theCompletionHandler interfaces such as:
-
NSURLSessionDownloadTask* downloadTask = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
-
}];
2.
-
NSURLSession* session = [NSURLSessionsessionWithConfiguration:[NSURLSessionConfiguration
-
defaultSessionConfiguration]delegate:selfdelegateQueue:nil];
This method has been setNSURLSession delegate, so it is expected that the returned response and data will be processed in a proxy manner, however, it is important to note that if the Task is created using a proxy with acompletionHandler parameter, then the response will still be in thecom, the processing is done in the block of the PletionHandler and does not go through the proxy interface. Therefore, if the response is guaranteed to go through the proxy interface, use a block with nointerface of the completionHandler parameter, or by setting theThe block of completionHandler is set to nil.
II. Response thread of execution issues:
1. For the created task, if its response is handled in the way described above by theThe way blocks are handled in the completionHandler:
1) IfThe session is created with theNSURLSession* session = [NSURLSession sharedSession], then regardless of whether the thread in which session executes is the main thread or a subthread, the thread in which the code in the block executes is an arbitrarily chosen subthread.
2) If the session is created by
-
NSURLSession* session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration
-
defaultSessionConfiguration] delegate:self delegateQueue:nil],
asdelegateQueue = nil,Then, regardless of whether the session execution thread is the main thread or a subthread, the code execution thread in the block is an arbitrarily chosen subthread;
asdelegateQueue = [NSOperationQueue mainQueue],Then, regardless of whether the thread in which the session is executed is the main thread or a sub-thread, theimitateThe code in the block is executed in the main thread;
asdelegateQueue = [[NSOperationQueue alloc]init],Then, regardless of whether the session execution thread is the main thread or a subthread, the code execution thread in the block is an arbitrarily chosen subthread;
2. For the created task, if its response is handled in such a way as to excuse it through the above delegate proxyThe way it is handled:
If delegateQueue = nil, the code execution thread in the block is an arbitrarily chosen subthread, regardless of whether the session execution thread is the main thread or a subthread;
If delegateQueue = [NSOperationQueue mainQueue], the code in the block is executed in the main thread, regardless of whether the session is executed in the main thread or a sub-thread;
asdelegateQueue = [[NSOperationQueue alloc] init], then the thread of code execution in the block is an arbitrarily chosen subthread, regardless of whether the session execution thread is the main thread or a subthread;
III. ForNSURLSession, when it opens a task in a sub-thread and processes the response through a proxy, there is no need to manually open the runloop of this thread here, unlike NSURLConnection.