Re: some advice on asynchronous control please
Re: some advice on asynchronous control please
- Subject: Re: some advice on asynchronous control please
- From: Ken Thomases <email@hidden>
- Date: Fri, 19 Feb 2010 16:22:23 -0600
On Feb 19, 2010, at 4:06 PM, Damien Cooke wrote:
> I have a situation where I am using a 3rd party library to manage content on their service. When I delete an entry the method requires a call back all good. Now I need to wait for that call back to be called to know if it was successful or not. In the call back I can set a boolean to indicate success or not but waiting for that to be set is my issue. What is the right way to wait for it?
>
> I think
>
> while(!done)
> {
> [NSThread sleepForTimeInterval:1];
> }
>
> Just blocks the main thread which is not what I want at all as this also blocks the run loop also.
>
> Can anyone give me some guidance on the best practice for this situation?
The best thing is to restructure your code. Split it into the part that can happen before the callback and the part that has to happen afterward. Once you've done the part that can happen before, simply return out of your code back to the main event loop. When the callback happens, simply resume with the part that happens afterward.
If you can't do the 'afterward' part while you're being called back from the library, you can use -performSelector:withObject:afterDelay: with a 0 delay. That will allow control to return to the library and arrange for your 'afterward' part to be done when control returns to the main event loop.
If the library issues its callback on a background thread, you can use -performSelectorOnMainThread:withObject:waitUntilDone: instead. That will invoke your 'afterward' method on the main thread, when it's idle in the event loop. You would pass NO for the 'wait' parameter to allow control to return to the library immediately in the background thread.
Cheers,
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