On May 10, 2012, at 3:42 PM, Dave wrote:

> We are using a third party library that performs tasks asynchronously. This 
> is ok most of the time, but on some occasions I'd like to be able to wait 
> (not on the main thread) until an operation completes,

This is usually an indication of a design problem.


> Is there a way of doing this and if so I'd be really grateful if someone 
> could show me the code. I've tried using NSLock and NSConditionalLock etc. 
> but can't get it to work!

NSConditionLock should work.  You create it with a condition which you 
arbitrarily decide means "unsignaled".  The block locks it (without regard to 
condition) and then immediately unlocks it in a different condition which you 
decide means "signaled".  The code that needs to wait just locks it when the 
condition is signaled, unlocks it, and releases it.

Alternatively, you can use a dispatch_semaphore or dispatch_group for this.  
Here's an implementation using dispatch_group (written in main, not tested):


-(void) doAtomicOperation
{
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self.mLibraryObject doOperation:myOperation 
withCompletionBlock:^(void)(ResponseObject* theResponseObject)
                {
                        // Do work
                        dispatch_group_leave(group);
                }];

//**
//**    Wait for Signal before continuing
//**
                dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
                dispatch_release(group);
}

Regards,
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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

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

Reply via email to