DispatchSemaphore
You can use integrate DispatchSemaphore on execute method.
ExampleService.swift
import VFNetwork
import Foundation
class HomeService: RequestService<HomeAPI> {
let semaphore = DispatchSemaphore(value: 1)
func getJokes(completion: @escaping (Result<HomeModel, Error>, _ semaphore: DispatchSemaphore) -> Void) {
execute(.home, responseType: HomeModel.self, semaphore: semaphore, completion: completion)
}
func getCategories(completion: @escaping (Result<HomeCategories, Error>, _ semaphore: DispatchSemaphore) -> Void) {
execute(.categories, responseType: HomeCategories.self, semaphore: semaphore, completion: completion)
}
}
and execute in your viewModel using leave() to terminate group independent execution.
ExampleViewModel.swift
func getJokes(completion: @escaping (Result<HomeModel, Error>) -> Void) {
service.getJokes { result, semaphore in
semaphore.signal()
switch result {
case let .success(joke):
completion(.success(joke))
case let .failure(error):
completion(.failure(error))
}
}
}
func getCategories() {
service.getCategories { result, semaphore in
semaphore.signal()
switch result {
case .success:
break
case let .failure(error):
print(error)
}
}
}
Thats it