Short answer: The main observation is that the URLSessionTask always runs asynchronously with respect to the thread that you started it. And unless you explicitly specify otherwise, completion handlers and / or delegation methods will execute in the background thread. Thus, you do not need to use GCD when starting the request, but in the completion handler we will use GCD to send everything that updates the interface or model in the main queue.
You asked:
- What is the
URLSessionTask stream? Main thread or background thread?
There are two questions here: which stream (s) of the URLSession uses internally for its own purposes and which processes the completion handler and / or delegation methods.
On the first question, this is an internal implementation detail that is not documented anywhere, but seems to create its own thread (background) with a separate run loop to process requests. But these implementation details do not really matter: we are sure that the request is executed asynchronously (does not block the current thread).
The last question that calls the flow of completion handlers and delegation methods is generally much more important. Unless otherwise specified, URLSession launches completion handlers and delegation methods in the serial operation queue created for us by URLSession . This means that they are running on a background thread.
The only exception to this rule is if you specified OperationQueue.main as the queue parameter when creating the URLSession instance, in which case it will obviously use the main thread for completion handlers and delegation methods. But even in this case, the request is executed asynchronously, and URLSession will not block the main thread.
- Why does the current thread show null in the stream name? Does this mean that it works in the background thread by default? (I see name = "main" to print in the main topic)
He works in a queue of sequential operations. Threads used by work queue threads usually have no names. But you can look at OperationQueue.current?.name to confirm which operational queue is being used.
- In general, do I need to run a
URLSessionTask with a GCD to make it work in the background thread or not? I ask about this because I saw that some tutorials do not use GCD to run the URLSessionTask , they use GCD to run the completion handler in the main thread.
The flow suggested by these tutorials is correct. When you run the query you do not need to use GCD. It always works asynchronously with respect to the queue with which you started it. The only thing you need to do is send the appropriate code in the completion handler or delegate the method to the appropriate queue.
In particular, since we usually allow URLSession to run completion handlers in its own sequential queue, so we need to send UI updates back to the main queue. Sometimes ignored, we also usually send model updates back to the main queue (or use some other synchronization mechanism).
Rob Aug 02 '17 at 15:00 2017-08-02 15:00
source share