iosswiftmultithreadingnsoperation

iOS multiple API Calls for single VC - best practice


I'm using Swift 3.0, I have a requirement where my VC displays screen based on 4API calls. Other VC has 2 API Calls.

As of now I created a background thread and running it serially. There is no dependence on other API calls. All are independent.

I somehow feel that this is not right way. Searched forum's but couldn't find exact answer.

What is the best practice that I can use to call all 4 API's ?

Using GCD Parallel queues or with NSOperation? Please advise.


Solution

  • You can use Dispatch group and call all api parallel. Once all executions are finished you can use notify method to perform other task.

    group.notify(queue: DispatchQueue.global(qos: .background)) {
          print("All async calls were finished, do our stuff here")
        }