Can AFNetworking return data synchronously (inside a block)?

To block the execution of the main thread until the operation completes, you could do [operation waitUntilFinished] after it’s added to the operation queue. In this case, you wouldn’t need the return in the block; setting the __block variable would be enough. That said, I’d strongly discourage forcing asynchronous operations to synchronous methods. It’s tricky … Read more

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

Short answer Instead of accessing self directly, you should access it indirectly, from a reference that will not be retained. If you’re not using Automatic Reference Counting (ARC), you can do this: __block MyDataProcessor *dp = self; self.progressBlock = ^(CGFloat percentComplete) { [dp.delegate myAPI:dp isProcessingWithProgress:percentComplete]; } The __block keyword marks variables that can be modified … 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

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