On Aug 26, 2011, at 11:40 PM, Brian Norh wrote:

> I'm working against an API which looks like this:
> 
> - (void)performOperationWithBlock:(void (^)(void))block
> 
> The call returns directly and the execution continues. At some point
> the block is eventually called. How would I do to block execution
> until the block have been called?
> 
> [obj performOperationWithBlock:^(void) {
>    // Do something here
> }];
> // Block here until the block above have been called.

If you can, put the work that you want to do after the block is called into the 
block itself, perhaps dispatched to the main queue if necessary.

If you can't, use a synchronization primitive to wait.  For example, you can 
create a dispatch semaphore with an initial value of 0, signal it inside the 
block, and wait on it after the block has been queued.  
(dispatch_semaphore_create, dispatch_semaphore_signal, and 
dispatch_semaphore_wait)

Similarly, you can create a dispatch group, call dispatch_group_enter() before 
-performOperationWithBlock:, call dispatch_group_leave() within the block, then 
call dispatch_group_wait() afterward.

NSConditionLock is another widely useful synchronization primitive with greater 
flexibility.

Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to