Waiting for a future runloop-based callback
Waiting for a future runloop-based callback
- Subject: Waiting for a future runloop-based callback
- From: Jens Alfke <email@hidden>
- Date: Wed, 08 Oct 2014 11:45:52 -0700
I thought I understood runloops pretty well, but I'm running into a situation that's got me stumped. Basically it involves how to wait (block) for a future message-send that will be queued onto the current runloop.
So I've started an asynchronous task that runs on a background thread. When the task completes, it's going to notify the originating thread via a call to -performSelector:onThread:....
In some circumstances the program later decides it need to block the originating thread until the operation completes. So it runs a nested runloop like this:
while (!taskIsComplete)
[[NSRunLoop currentRunLoop] runMode: @"myCustomMode"
beforeDate: [NSDate distantFuture]];
where taskIsComplete is some flag that will get set by the target method invoked from the task thread.
(I use a custom runloop mode because, if I used the default mode, the application would respond to user input and other events while waiting in the loop, resulting in re-entrant calls to the application code, causing all sorts of problems. In fact what I'm actually doing is fixing a bug in the current code where it incorrectly uses the default mode, leading to a crash.)
Over on the task thread, when it completes the task it calls:
[originator performSelector: @selector(taskDone:) onThread: originatingThread withObject: self
waitUntilDone: NO modes: @[@"myCustomMode"]];
where the -taskDone: method will set that taskIsComplete ivar that the wait code is checking.
Unfortunately this doesn't work, because the runloop immediately bails out and returns NO from -runMode:beforeDate:. Apparently this is because there are no input sources registered for myCustomMode. Which is true, but that doesn't mean that no events are going to arrive for that mode! The problem seems to be that this part of the runloop implementation doesn't understand about delayed-perform-in-mode calls.
What's the workaround? Should I add some sort of no-op input source to the runloop for my custom mode? That smells very hacky.
—Jens
_______________________________________________
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