iPhone – Grand Central Dispatch main thread

Dispatching a block to the main queue is usually done from a background queue to signal that some background processing has finished e.g. – (void)doCalculation { //you can use any string instead “com.mycompany.myqueue” dispatch_queue_t backgroundQueue = dispatch_queue_create(“com.mycompany.myqueue”, 0); dispatch_async(backgroundQueue, ^{ int result = <some really long calculation that takes seconds to complete>; dispatch_async(dispatch_get_main_queue(), ^{ [self … Read more

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

Since the beginning, Swift has provided some facilities for making ObjC and C more Swifty, adding more with each version. Now, in Swift 3, the new “import as member” feature lets frameworks with certain styles of C API — where you have a data type that works sort of like a class, and a bunch … Read more

Waiting until two async blocks are executed before starting another block

Use dispatch groups: see here for an example, “Waiting on Groups of Queued Tasks” in the “Dispatch Queues” chapter of Apple’s iOS Developer Library’s Concurrency Programming Guide Your example could look something like this: dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { // block1 NSLog(@”Block1″); [NSThread sleepForTimeInterval:5.0]; NSLog(@”Block1 End”); }); dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { // … Read more

NSOperation vs Grand Central Dispatch

GCD is a low-level C-based API that enables very simple use of a task-based concurrency model. NSOperation and NSOperationQueue are Objective-C classes that do a similar thing. NSOperation was introduced first, but as of 10.5 and iOS 2, NSOperationQueue and friends are internally implemented using GCD. In general, you should use the highest level of … Read more

How do you trigger a block after a delay, like -performSelector:withObject:afterDelay:?

I think you’re looking for dispatch_after(). It requires your block to accept no parameters, but you can just let the block capture those variables from your local scope instead. int parameter1 = 12; float parameter2 = 144.1; // Delay execution of my block for 10 seconds. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ NSLog(@”parameter1: %d parameter2: … Read more

How do I wait for an asynchronously dispatched block to finish?

Trying to use a dispatch_semaphore. It should look something like this: dispatch_semaphore_t sema = dispatch_semaphore_create(0); [object runSomeLongOperationAndDo:^{ STAssert… dispatch_semaphore_signal(sema); }]; if (![NSThread isMainThread]) { dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); } else { while (dispatch_semaphore_wait(sema, DISPATCH_TIME_NOW)) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0]]; } } This should behave correctly even if runSomeLongOperationAndDo: decides that the operation isn’t actually long enough … Read more