On Jan 27, 2012, at 2:14 PM, Jens Alfke wrote:
> I'm really used to using -performSelector:withObject:afterDelay: to make
> something happen later. But I'd much rather use a block than a target/action.
> I can't find any API for this, however. Am I missing something? What I want
> is basically like
> PerformBlockAfterDelay(^{ …code here…}, 5.0);
>
> It looks like I should just call dispatch_async, but I'm unsure which
> dispatch queue to use. dispatch_get_main_queue only works for the main
> thread. Can I use dispatch_get_current_queue? I'm unsure of what this does
> when called on a background thread. The API docs say "If the call is made
> from any other thread, this function returns the default concurrent queue" …
> is that a queue associated with the thread's runloop, that's guaranteed to
> execute tasks on that thread?
There's no such thing as a dispatch queue for the current thread's run loop.
The main thread's run loop also runs the main queue, as you've noted, but no
other thread's run loop also runs a dispatch queue. In fact, in general, no
other queue runs on any of your threads. libdispatch manages its own worker
threads.
On Jan 27, 2012, at 4:19 PM, Marco Tabini wrote:
> I just use a little category on NSObject that I think I pulled from
> somewhere—but I can't remember where from:
> - (void) performBlock:(void (^)(void)) block afterDelay:(NSTimeInterval)
> delay {
> block = [block copy];
>
> [self performSelector:@selector(fireBlockAfterDelay:)
> withObject:block
> afterDelay:delay];
> }
>
> - (void)fireBlockAfterDelay:(void (^)(void))block {
> block();
> }
Believe it or not, this also works:
[(id)^{ ... code here ... } performSelector:@selector(invoke)
withObject:nil afterDelay:5.0];
That is, you can target performSelector:withObject:afterDelay: at a block,
itself, rather than some other helper object. And, a block implements the
-invoke selector to, well, invoke itself.
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list ([email protected])
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 [email protected]