Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda