Skip navigation.
 
mlRe: Is there another way to do performSelectorOnMainThread?
FROM : Shaun Wexler
DATE : Fri Jun 30 21:59:07 2006

On Jun 30, 2006, at 10:20 AM, Jerrod Fowkes wrote:

> JAnd a real pain!  After using NSInvocation a few times, I realized 
> "Why am I
> putting myself through this torture?"


Allow me to relieve you...  ;)

@interface SKWMainThreadProxy : NSProxy
{
    id realObject;
    BOOL isAsync;
}

- (id)initWithObject:(id)object isAsync:(BOOL)async;

@end

@implementation SKWMainThreadProxy

- (id)initWithObject:(id)object isAsync:(BOOL)async
{
    realObject = [object retain];
    isAsync = async;
    return self;
}

- (void)dealloc
{
    [realObject release];
    [super dealloc];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
    return [realObject methodSignatureForSelector:selector];
}

- (void)forwardInvocation:(NSInvocation *)invocation
{
    [invocation setTarget:realObject];

    if (pthread_main_np()) {
     [invocation invoke];
    } else {
        [invocation retainArguments];
        if (!isAsync) {
            [invocation performSelectorOnMainThread:@selector
(invoke) withObject:nil waitUntilDone:YES];
        } else {
            [self performSelectorOnMainThread:@selector(invoke:) 
withObject:invocation waitUntilDone:NO];
        }
    }
}

- (void)invoke:(NSInvocation *)invocation
{
    [invocation invoke];
}

+ (NSString *)description
{
    return [[super description] 
stringByAppendingString:@"_SKWMainThreadProxy"];
}

- (NSString *)description
{
    return [realObject description];
}

@end

--
Shaun Wexler
MacFOH
http://www.macfoh.com

"No problem is so formidable that you can't walk away from it." - 
Charles Schulz

Related mailsAuthorDate
mlIs there another way to do performSelectorOnMainThread? Scott Andrew Jun 28, 20:55
mlRe: Is there another way to do performSelectorOnMainThread? Mark Stultz Jun 28, 20:56
mlRe: Is there another way to do performSelectorOnMainThread? Alan Pearson Jun 28, 21:01
mlRe: Is there another way to do performSelectorOnMainThread? Aurélien Hugelé Jun 29, 16:32
mlRe: Is there another way to do performSelectorOnMainThread? Finlay Dobbie Jun 29, 17:17
mlRe: Is there another way to do performSelectorOnMainThread? Dustin Voss Jun 30, 02:58
mlRe: Is there another way to do performSelectorOnMainThread? Jerry Krinock Jun 30, 17:28
mlRe: Is there another way to do performSelectorOnMainThread? Jerrod Fowkes Jun 30, 19:20
mlRe: Re: Is there another way to do performSelectorOnMainThread? Michael Ash Jun 30, 20:46
mlRe: Is there another way to do performSelectorOnMainThread? Shaun Wexler Jun 30, 21:59