• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to get the dispatch queue for the current thread's runloop?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to get the dispatch queue for the current thread's runloop?


  • Subject: Re: How to get the dispatch queue for the current thread's runloop?
  • From: Steve Sisak <email@hidden>
  • Date: Sat, 28 Jan 2012 12:23:58 -0500

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 (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


  • Follow-Ups:
    • Re: How to get the dispatch queue for the current thread's runloop?
      • From: Steve Sisak <email@hidden>
References: 
 >How to get the dispatch queue for the current thread's runloop? (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Blocks and Methods...
  • Next by Date: Re: Full-Height Toolbar Item
  • Previous by thread: Re: How to get the dispatch queue for the current thread's runloop?
  • Next by thread: Re: How to get the dispatch queue for the current thread's runloop?
  • Index(es):
    • Date
    • Thread