Skip to content

Instantly share code, notes, and snippets.

@MonsieurDart
Created March 18, 2011 09:23
Show Gist options
  • Save MonsieurDart/875812 to your computer and use it in GitHub Desktop.
Save MonsieurDart/875812 to your computer and use it in GitHub Desktop.
Performing a Multi Parameter Method in Background in ObjC
//
// NSObject+Performer.h
// EurActiv
//
// Created by Mathieu Godart on 21/01/11.
// Copyright 2011 L'atelier du mobile. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSObject (Performer)
// Executed in background, this method will invoke the given invocation and release its arguments, if needed.
// Caution: all arguments must respond to release.
- (void)createPoolAndInvoke:(NSInvocation *)invocation;
// Invokes a method of the receiver on a new background thread, already populated with an NSAutoreleasePool.
// No need for the performed selector to handle the autorelease pool allocation. It also retains the argument
// objects until the call to aSelector.
// Throws an exception if the selector has an incorrect signature.
- (void)performSelectorInPooledBackground:(SEL)aSelector withObject:(id)arg1 withObject:(id)arg2 withObject:(id)arg3;
@end
//
// NSObject+Performer.m
// EurActiv
//
// Created by Mathieu Godart on 21/01/11.
// Copyright 2011 L'atelier du mobile. All rights reserved.
//
#import "NSObject+Performer.h"
@implementation NSObject (Performer)
// Executed in background, this method will invoke the given invocation and release its arguments, if needed.
// Caution: all arguments must respond to release.
- (void)createPoolAndInvoke:(NSInvocation *)invocation {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (![invocation.target respondsToSelector:invocation.selector]) {
[NSException raise:@"Invalid target selector couple"
format:@"target %@ doesn't respond to %@",
invocation.target, NSStringFromSelector(invocation.selector)];
}
[invocation invoke];
[pool release];
// After that the invocation will be released, and so will be its arguments.
}
// Invokes a method of the receiver on a new background thread, already populated with an NSAutoreleasePool.
// No need for the performed selector to handle the autorelease pool allocation. It also retains the argument
// objects until the call to aSelector.
// Throws an exception if the selector has an incorrect signature.
- (void)performSelectorInPooledBackground:(SEL)aSelector withObject:(id)arg1 withObject:(id)arg2 withObject:(id)arg3 {
NSMethodSignature *sign = [self methodSignatureForSelector:aSelector];
if (!sign) [NSException raise:@"Invalid selector signature" format:@"signature of %@ is invalid",
NSStringFromSelector(aSelector)];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sign];
[invocation setTarget:self];
[invocation setSelector:aSelector];
if (sign.numberOfArguments > 2) [invocation setArgument:&arg1 atIndex:2];
if (sign.numberOfArguments > 3) [invocation setArgument:&arg2 atIndex:3];
if (sign.numberOfArguments > 4) [invocation setArgument:&arg3 atIndex:4];
[invocation retainArguments];
[self performSelectorInBackground:@selector(createPoolAndInvoke:) withObject:invocation];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment