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 (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: http://lists.apple.com/mailman/options/cocoa-dev/tonyrom%40hotmail.com This email sent to tony...@hotmail.com _______________________________________________ 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com