Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save teodorpatras/87f6b8918679043f62ba to your computer and use it in GitHub Desktop.
Save teodorpatras/87f6b8918679043f62ba to your computer and use it in GitHub Desktop.
- (void) performSelectorAsync:(SEL) selector completion:(void(^)(id result))completion {
if ([self respondsToSelector:selector]) {
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:self];
__unsafe_unretained id unretainedResult = nil;
[invocation invoke];
[invocation getReturnValue:&unretainedResult];
id result = unretainedResult;
dispatch_async(dispatch_get_main_queue(), ^(void){
//Main Thread
completion(result);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment