How do I wait for an asynchronously dispatched block to finish?
问题 I am testing some code that does asynchronous processing using Grand Central Dispatch. The testing code looks like this: [object runSomeLongOperationAndDo:^{ STAssert… }]; The tests have to wait for the operation to finish. My current solution looks like this: __block BOOL finished = NO; [object runSomeLongOperationAndDo:^{ STAssert… finished = YES; }]; while (!finished); Which looks a bit crude, do you know a better way? I could expose the queue and then block by calling dispatch_sync :