0

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

            }
        }  
EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126

1 Answers1

1
//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.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610