Re: Threads and Locking Question
Re: Threads and Locking Question
- Subject: Re: Threads and Locking Question
- From: Dave <email@hidden>
- Date: Thu, 10 May 2012 22:44:55 +0100
Hi Ken,
Thanks for that I will try it out tomorrow, as an aside, is this ok/
recommended?
-(void) someMethod:(NSString*) theParameter
{
if ([NSThread isMainThread)
{
[self performSelectorInBackground:@selector(someMethod:)
withObject:self];
return;
}
// Do work
}
Thanks again
Dave
On 10 May 2012, at 22:09, Ken Thomases wrote:
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 (email@hidden)
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:
This email sent to email@hidden