Wait until swift for loop with asynchronous network requests finishes executing

I would like a for in loop to send off a bunch of network requests to firebase, then pass the data to a new view controller once the the method finishes executing. Here is my code: var datesArray = [String: AnyObject]() for key in locationsArray { let ref = Firebase(url: “http://myfirebase.com/” + “\(key.0)”) ref.observeSingleEventOfType(.Value, withBlock: … Read more

Waiting until two async blocks are executed before starting another block

When using GCD, we want to wait until two async blocks are executed and done before moving on to the next steps of execution. What is the best way to do that? We tried the following, but it doesn’t seem to work: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { // block1 }); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { // block2 … Read more

In Swift how to call method with parameters on GCD main thread?

In my app I have a function that makes an NSRURLSession and sends out an NSURLRequest using sesh.dataTaskWithRequest(req, completionHandler: {(data, response, error) In the completion block for this task, I need to do some computation that adds a UIImage to the calling viewcontroller. I have a func called func displayQRCode(receiveAddr, withAmountInBTC:amountBTC) that does the UIImage-adding … Read more

How do I dispatch_sync, dispatch_async, dispatch_after, etc in Swift 3, Swift 4, and beyond?

I have lots of code in Swift 2.x (or even 1.x) projects that looks like this: // Move to a background thread to do some long running work dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let image = self.loadOrGenerateAnImage() // Bounce back to the main thread to update the UI dispatch_async(dispatch_get_main_queue()) { self.imageView.image = image } } Or stuff … Read more

How do I write dispatch_after GCD in Swift 3, 4, and 5?

In Swift 2, I was able to use dispatch_after to delay an action using grand central dispatch: var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) dispatch_after(dispatchTime, dispatch_get_main_queue(), { // your function here }) But this no longer seems to compile since Swift 3. What is the preferred way to write this in modern Swift? 12 … Read more

NSOperation vs Grand Central Dispatch

I’m learning about concurrent programming for iOS. So far I’ve read about NSOperation/NSOperationQueue and GCD. What are the reasons for using NSOperationQueue over GCD and vice versa? Sounds like both GCD and NSOperationQueue abstract away the explicit creation of NSThreads from the user. However the relationship between the two approaches isn’t clear to me so … Read more

dispatch_after – GCD in Swift?

I’ve gone through the iBook from Apple, and couldn’t find any definition of it: Can someone explain the structure of dispatch_after? dispatch_after(<#when: dispatch_time_t#>, <#queue: dispatch_queue_t?#>, <#block: dispatch_block_t?#>) 26 s 26 A clearer idea of the structure: dispatch_after(when: dispatch_time_t, queue: dispatch_queue_t, block: dispatch_block_t?) dispatch_time_t is a UInt64. The dispatch_queue_t is actually type aliased to an NSObject, … Read more