How to create dispatch queue in Swift 3

Creating a concurrent queue let concurrentQueue = DispatchQueue(label: “queuename”, attributes: .concurrent) concurrentQueue.sync { } Create a serial queue let serialQueue = DispatchQueue(label: “queuename”) serialQueue.sync { } Get main queue asynchronously DispatchQueue.main.async { } Get main queue synchronously DispatchQueue.main.sync { } To get one of the background thread DispatchQueue.global(qos: .background).async { } Xcode 8.2 beta 2: … Read more

Wait until swift for loop with asynchronous network requests finishes executing

You can use dispatch groups to fire an asynchronous callback when all your requests finish. Here’s an example using dispatch groups to execute a callback asynchronously when multiple networking requests have all finished. override func viewDidLoad() { super.viewDidLoad() let myGroup = DispatchGroup() for i in 0 ..< 5 { myGroup.enter() Alamofire.request(“https://httpbin.org/get”, parameters: [“foo”: “bar”]).responseJSON { … Read more

dispatch_after – GCD in Swift?

I use dispatch_after so often that I wrote a top-level utility function to make the syntax simpler: func delay(delay:Double, closure:()->()) { dispatch_after( dispatch_time( DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)) ), dispatch_get_main_queue(), closure) } And now you can talk like this: delay(0.4) { // do stuff } Wow, a language where you can improve the language. What could … Read more