Created
August 4, 2012 11:53
-
-
Save 5sw/3256982 to your computer and use it in GitHub Desktop.
QueueProxy - call delegate methods on a different operation queue. Useful as a replacement for [NSURLConnection setDelegateQueue:]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface QueueProxy : NSProxy | |
+ (id) proxyForDelegate: (id)delegate onQueue: (NSOperationQueue *)queue; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "QueueProxy.h" | |
@implementation QueueProxy { | |
id _delegate; | |
NSOperationQueue *_queue; | |
} | |
+ (id)proxyForDelegate:(id)delegate onQueue:(NSOperationQueue *)queue; | |
{ | |
QueueProxy *result = [self alloc]; | |
result->_delegate = delegate; | |
result->_queue = [queue retain]; | |
return [result autorelease]; | |
} | |
- (void)dealloc; | |
{ | |
[_queue release]; | |
[super dealloc]; | |
} | |
- (id)forwardingTargetForSelector:(SEL)aSelector | |
{ | |
if ([[NSOperationQueue currentQueue] isEqual: _queue]) { | |
return _delegate; | |
} | |
return nil; | |
} | |
- (void)forwardInvocation:(NSInvocation *)invocation; | |
{ | |
[invocation retainArguments]; | |
[invocation setTarget: self->_delegate]; | |
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation: invocation]; | |
[self->_queue addOperation: operation]; | |
NSMethodSignature *signature = invocation.methodSignature; | |
if (strcmp( signature.methodReturnType, @encode( void ) ) != 0) { | |
[operation waitUntilFinished]; | |
} | |
[operation release]; | |
} | |
- (BOOL)respondsToSelector:(SEL)aSelector; | |
{ | |
if ([super respondsToSelector: aSelector]) return YES; | |
return [_delegate respondsToSelector: aSelector]; | |
} | |
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel; | |
{ | |
return [_delegate methodSignatureForSelector: sel]; | |
} | |
- (BOOL)conformsToProtocol:(Protocol *)aProtocol; | |
{ | |
if ([super conformsToProtocol: aProtocol]) return YES; | |
return [_delegate conformsToProtocol: aProtocol]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment