At 12:14 PM -0800 1/27/12, 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);

Me too! PerformBlockOnThread would be nice as well.

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?

As others have mentioned later in the thread, with the exception of the main queue, dispatch queues aren't tied to threads -- they execute blocks on a pool of threads managed by the system.

Later in the thread:

At 6:00 PM +1100 1/28/12, Shane Stanley wrote:
On 28/01/2012, at 1:41 PM, Ken Thomases wrote:

 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.

FWIW, on stackoverflow there's a post suggesting the use of a block as a target, and using -invoke to select it. In the comments is one from bbum from last October:

This "works" by coincidence. It relies on private API; the invoke method on Block objects is not public and not intended to be used in this fashion.

<http://stackoverflow.com/questions/4581782/can-i-pass-a-block-as-a-selector-with-objective-c>, about fourth answer down.

This reminds me that, IIRC, a block (after it's copied to the heap) _is_ an object. I was going to suggest some form of calling BlockCopy manually, but given the example above, you might something like:

-(void)executeBlock:((^)(void) block  // <- someone help with this declaration
{
  block();
}

...

[self performSelector:@selector(executeBlock:) withObject:(id)^{ ... code here ... } afterDelay:5.0];

might work.

I'm still trying to get my head around all these issues as well, but hoping this might help you to a solution. I'm interested in the solution as well.

HTH,

-Steve


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to