Re: waiting for async callback in main thread
Re: waiting for async callback in main thread
- Subject: Re: waiting for async callback in main thread
- From: John Engelhart <email@hidden>
- Date: Thu, 29 Oct 2009 01:17:20 -0400
On Wed, Oct 28, 2009 at 7:08 AM, Dave Keck <email@hidden> wrote:
>
> I've hit several other brick walls from recursive run loops, but they
> escape me at the moment. Nonetheless, recursive run loops are a fancy
> way of blocking, and are contrary to the event-driven model that we
> know and love.
>
> I guess the rule that I stick by is "Don't use recursive run loops.
> But If you do, never (ever!) run them in anything but a custom run
> loop mode."
Well, just to be clear, I'm not saying "Don't do this", I said that I
"strongly advise against doing this." As both you, I, and others have
pointed out, there can be some unexpected consequences to running a run loop
recursively. I'd categorize it as an "advanced technique for the
experienced Objective-C / Cocoa programmer". No arguing that you can "get
it right", but it can be tricky and cause unexpected, non-intuitive
side-effects. Particularly for beginning and intermediate Cocoa
programmers.
I guess I could summarize my position as "Try to solve the problem 'the
right way' (for some vaguely defined value of 'right way') before trying to
solve the problem by recursively running the run loop, particularly the main
run loop." Recursively running a run loop on your own thread is probably an
entirely different matter all together- you're in complete control and
you've (probably) specifically programmed things with that fact in mind.
The main run loop, on the other hand, has the potential to be a big can of
worms.
If you are forced to run the main run loop recursively, strive for
"simplicity". As others have mentioned (Kyle Sluder), one way to do this is
to run the run loop in your own, custom run loop mode. This is probably
your best option and it avoids the bulk of the problems I can think of. Of
course, this assumes you can get whatever it is you want to wait on to
deliver events in that custom run loop mode. It's been my experience that
API support for specifying run loop modes in third party stuff (which the OP
said he/she was using) is kind of hit or miss, so this might not be an
option. If the third party API doesn't support a run loop modes argument,
it's probably going to deliver 'events' in NSRunLoopCommonModes. If that's
the case, then IMHO, I'd try really, really hard to find an alternate
solution rather than trying to run the main run loop recursively
in NSRunLoopCommonModes. While I'm sure you COULD get it to work, my guess
is that it's not going to be worth the effort and likely to be brittle,
especially in the face of "major" changes that can happen between major OS
releases.
But lets face it, sometimes you're forced in to a corner and you just gotta.
:(
_______________________________________________
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