Skip to content

Instantly share code, notes, and snippets.

@SysCall97
Last active April 9, 2024 06:46
Show Gist options
  • Save SysCall97/bd1b64352de981a7bf0b9aca75429cb6 to your computer and use it in GitHub Desktop.
Save SysCall97/bd1b64352de981a7bf0b9aca75429cb6 to your computer and use it in GitHub Desktop.
import UIKit
class SplashViewController: UIViewController {
var launchDataDispatchGroup: DispatchGroup = DispatchGroup()
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global().async { [weak self] in
self?.getAppLaunchData()
}
}
private func getAppLaunchData() {
launchDataDispatchGroup.enter()
NetworkManager.shared.getData(for: .appConfig) { response in
launchDataDispatchGroup.leave()
// whatever you want to do with the response
}
launchDataDispatchGroup.enter()
NetworkManager.shared.getData(for: .user) { response in
launchDataDispatchGroup.leave()
// whatever you want to do with the response
}
launchDataDispatchGroup.enter()
NetworkManager.shared.getData(for: .longResponseTimeAPI) { response in
launchDataDispatchGroup.leave()
// whatever you want to do with the response
}
// Timeout for 3 seconds
let waitResult: DispatchTimeoutResult = launchDataDispatchGroup.wait(timeout: .now() + .seconds(3))
DispatchQueue.main.async { [weak self] in
switch waitResult {
case .success:
// API calls before 3 second timeout
case .timedOut:
// APIs timedout
}
// required instruction
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment