Skip navigation.
 
mlOdd behaviour with NSInvocation, performSelectorOnMainThread and NSClassFromString
FROM : Duncan McGregor
DATE : Fri Nov 16 23:26:42 2007

Please bear with me, I'm only a Java programmer.

I'm looking to capture the result of performSelectorOnMainThread, and 
in particular I'm trying to invoke NSClassFromString, in order to 
ensure that I build load QTMovie on the main thread.

With a little help from our Cocoa guys, I've come up with the following

@implementation SelectorHelper : NSObject
+ (id) performSelectorOnMainThread:(SEL) aSelector onTarget:
(NSObject*) theTarget withObject:(id) arg {
   NSString* selectorName = aSelector != nil ? NSStringFromSelector
(aSelector) : nil;
   NSString* className = NSStringFromClass([theTarget class]);
   NSLog(@"Calling performSelectorOnMainThread: %@ onTarget: %@ 
withObject: %@", selectorName, theTarget, arg);
   NSMethodSignature* methodSignature  = [theTarget 
methodSignatureForSelector: aSelector];
   if (!methodSignature) {
       NSLog(@"No methodSignature found");
       return nil;
   } else {
       NSLog(@"methodSignature : %@", methodSignature);
   }
   NSInvocation* invocation = [NSInvocation 
invocationWithMethodSignature: methodSignature];
   [invocation setTarget: theTarget];
   [invocation setSelector: aSelector];
   if (arg) {
       [invocation setArgument:&arg atIndex:2];
   }
   [invocation performSelectorOnMainThread: @selector(invoke) 
withObject: nil waitUntilDone:YES];
   void* result = malloc([methodSignature methodReturnLength]); // TODO
   [invocation getReturnValue: result];
   NSLog(@"performSelectorOnMainThread returning %@", result);
   return result;
}

+ (Class) createClass:(NSString*) className {
   Class result = NSClassFromString(className);
   NSLog(@"createClass returning %@ of type %@", result, [result class]);
   return result;
}

@end

Invoking [SelectorHelper performSelectorOnMainThread: @selector
(createClass) onTarget:SelectorHelper withObject:@"QTMovie"]
gives the following log

Calling performSelectorOnMainThread: createClass: onTarget: 
SelectorHelper withObject: QTMovie
methodSignature : NSMethodSignature: types=#@:@ nargs=3 
sizeOfParams=12 returnValueLength=4;
createClass returning QTMovie of type QTMovie
performSelectorOnMainThread returning <QTMovie: 0x323930 time scale = 
0, duration = 0, rate = 0.000000, tracks = { }>

As I read this (and looking in the debugger), createClass is 
returning a Class for QTMovie, but the result of the NSInvocation is 
an instance of QTMovie.

Does this make sense to anyone? I fear I must be doing something 
stupid, but cannot see what.

Thanks in anticipation

Duncan McGregor

Related mailsAuthorDate
mlOdd behaviour with NSInvocation, performSelectorOnMainThread and NSClassFromString Duncan McGregor Nov 16, 23:26
mlRe: Odd behaviour with NSInvocation, performSelectorOnMainThread and NSClassFromString Peter Ammon Nov 16, 23:49
mlRe: Odd behaviour with NSInvocation, performSelectorOnMainThread and NSClassFromString Duncan McGregor Nov 17, 01:13