Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda