• 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: Making an NSMethodInvocation from the current method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Making an NSMethodInvocation from the current method


  • Subject: Re: Making an NSMethodInvocation from the current method
  • From: Tony Romano <email@hidden>
  • Date: Sat, 21 Aug 2010 22:56:50 -0700

Use GCD.

               dispatch_sync(dispatch_get_main_queue(), ^{
                    // Call your function here
		MyFunction (param 1, param2, ...);
                });

Tony Romano
http://www.cocoaegghead.com




On Aug 21, 2010, at 10:03 PM, Roland King wrote:

Is there a built-in function to make an NSMethodInvocation from 'the current method I'm in with all current parameters', or does anyone have any code they've written to do this?

Motivation, I'm writing a display class which can get updated from a background thread, it has a whole load of methods, some of which don't lend themselves to performSelectorOnMainThread (some take more than two arguments, some take primitives and I don't really want to wrap and unwrap into NSNumbers all over the place). What I really would like is in each method to be able to write something like

if( ![ NSThread isMainThread ] )
	[ NSMagicFunctionReturningAnInvocationForThisCurrentFunction() performSelectorOnMainThread:@selector( invoke ) withObject:nil waitUntilDone:NO ];
else
{
	// method performing code here
}

but there is of course no such function I'm aware of nor can I easily think how I'd write such a thing.

I have a current solution for those methods which are properties using forwarding because forwardInvocation: is the only function I know of which gives me a pre-packaged invocation object but I find it a bit inelegant and it only works for properties. That method briefly works as follows, if I want a property 'foo', I declare it, then use @dynamic to suppress the compiler warnings. In the class continuation I declare the same property prepended with an given prefix (I'm using TS_ for threadsafe) and implement it. I then override forwardInvocation: and methodSignatureForSelector: to check for the existance of a method TS_<called selector> and if it exists I switch the selector in the NSInvocation forwardInvocation: gives me and invoke it if I'm on the main thread or forward it to the main thread if I'm not.

eg setFoo:123 is not implemented so methodSignatureForSelector: is called for setFoo: and I return the signature for TS_setFoo:. Then forwardInvocation: is called with a prepacked NSInvocation, I switch the selector to that for TS_setFoo: and invoke it.

This only works for properties because I can only use @dynamic to suppress the warnings on those, other declared methods in the interface need to be implemented (or is there a way to suppress that warning) and the whole TS_ prefix thing seems a bit hokey to me so I was looking for a more direct way to make an NSInvocation. _______________________________________________

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


_______________________________________________

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

References: 
 >Making an NSMethodInvocation from the current method (From: Roland King <email@hidden>)

  • Prev by Date: Making an NSMethodInvocation from the current method
  • Next by Date: Formatting float numbers
  • Previous by thread: Making an NSMethodInvocation from the current method
  • Next by thread: Re: Making an NSMethodInvocation from the current method
  • Index(es):
    • Date
    • Thread