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

How do I avoid capturing self in blocks when implementing an API?

I have a working app and I’m working on converting it to ARC in Xcode 4.2. One of the pre-check warnings involves capturing self strongly in a block leading to a retain cycle. I’ve made a simple code sample to illustrate the issue. I believe I understand what this means but I’m not sure the … Read more

Assign a variable inside a Block to a variable outside a Block

I’m getting an error Variable is not assignable (missing __block type specifier) on the line aPerson = participant;. How can I make sure the block can access the aPerson variable and the aPerson variable can be returned? Person *aPerson = nil; [participants enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { Person *participant = (Person*)obj; if ([participant.gender … Read more

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

Is there a way to call a block with a primitive parameter after a delay, like using performSelector:withObject:afterDelay: but with an argument like int/double/float? 20 s 20 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 … Read more