Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
What is the better way to call multiple services in iOS?

I have 5 different services requests to load into same UItableView for each cells.

What is the best way to approach in order to do this.

https://example.com/?api/service1
https://example.com/?api/service2
https://example.com/?api/service3
https://example.com/?api/service4
https://example.com/?api/service5

let url = "https://example.com/?api/service1
Alamofire.request(url, method: .get, parameters:nil encoding: JSONEncoding.default, headers: nil)
    .responseJSON { response in
        print(response.result.value as Any)   // result of response serialization
}

repeat the same Alamofire five times with different services name there is another way to implement it.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Look at using a DispatchGroup to perform multiple async requests and wait for them all to complete.

For each task you call group.enter() and in its completion handler when you know that request has finished you call group.leave(). Then there is a notify method which will wait for all requests to call leave to tell you that they have all finished.

I have created an example in a Playground (which will fail with errors because of the URL's used)

import UIKit
import PlaygroundSupport

let serviceLinks = [
    "https://example.com/?api/service1",
    "https://example.com/?api/service2",
    "https://example.com/?api/service3",
    "https://example.com/?api/service4",
    "https://example.com/?api/service5"
]

// utility as I've not got alamofire setup
func get(to urlString: String, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
    let url = URL(string: urlString)!
    let session = URLSession.shared
    let task = session.dataTask(with: url) { data, response, error in
        completion(data, response, error)
    }
    task.resume()
}

let group = DispatchGroup()

for link in serviceLinks {
    group.enter() // add an item to the list 
    get(to: link) { data, response, error in
        // handle the response, process data, assign to property
        print(data, response, error)
        group.leave() // tell the group your finished with this one
    }
}

group.notify(queue: .main) {
    //all requests are done, data should be set
    print("all done")
}

PlaygroundPage.current.needsIndefiniteExecution = true

You probably won't be able to just loop through the URL's like I have though because the handling of each service is probably different. You'll need to tweak it based on your needs.

There is alot more information about DispatchGroups available online such as this article

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!