EyeQ Tech
EyeQ Tech

Reputation: 7378

swift, calling network another time in the same task's thread

Sorry for beginner's question. I have an action that depends on the result of the data returned from the network, and the action may require another network request. Since the first network request is called in datatask already, I want to use that same thread for the 2nd network request, but don't know how to do it. Any tip? tks

 let task = URLSession.shared.dataTask(with: request  as URLRequest )
        {   data, response, error in

            if func_1 (data) {
                return
            }else {
                //call another network request here, but don't want to do 
                //another task = URLSession.shared.... again since we are already on an async thread

            }
        }  

Upvotes: 0

Views: 142

Answers (1)

Rob Napier
Rob Napier

Reputation: 299565

//call another network request here, but don't want to do 
//another task = URLSession.shared.... again since we are already on an async thread

This is a misunderstanding. There is no problem creating another network request at this point. All that does is place another request on the appropriate queue. It doesn't matter what thread you're running on when you schedule that.

I highly recommend Apple's Concurrency Programming Guide. You may have some misunderstandings about how iOS concurrency works. A key part of iOS work is understanding GCD queues, and particularly how they differ from threads.

Upvotes: 1

Related Questions