use blocks and gcd, or use a proxy
http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSProxy_Class/Reference/Reference.html
A proxy can forward all invocations to the main thread. Create a subclass of
NSProxy. Implement
– (void)forwardInvocation:(NSInvocation *)anInvocation
{
[anInvocation setTarget:realObject];
[anInvocation performSelectorOnMainThread:@selector(invoke) withObject:nil
waitUntilDone:NO ];
return;
}
See also Higher Order Messaging which uses proxies of a sort:
http://www.metaobject.com/papers/Higher_Order_Messaging_OOPSLA_2005.pdf
See also
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/DistrObjects/DistrObjects.html
On Aug 22, 2010, at 1:03 AM, 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 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:
> http://lists.apple.com/mailman/options/cocoa-dev/erik.buck%40sbcglobal.net
>
> This email sent to [email protected]
_______________________________________________
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]