Created
October 2, 2011 17:37
-
-
Save jamztang/1257678 to your computer and use it in GitHub Desktop.
Easily perform list of operations on specific NSOperationQueue through NSProxy
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
// | |
// JTInvocationManager.h | |
// | |
// Created by James Tang on 02/10/2011. | |
// | |
#import <Foundation/Foundation.h> | |
@interface JTInvocationManager : NSProxy { | |
id _proxiedTarget; | |
NSMutableArray *_invocations; | |
} | |
+ (id)invocationManager; | |
- (id)prepareWithInvocationTarget:(id)target; | |
- (void)removeAllInvocations; | |
- (NSArray *)operations; | |
- (void)invokeOnQueue:(NSOperationQueue *)queue waitUntilFinished:(BOOL)wait; | |
- (void)invoke; | |
@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
// | |
// JTInvocationManager.m | |
// | |
// Created by James Tang on 02/10/2011. | |
// | |
#import "JTInvocationManager.h" | |
@implementation JTInvocationManager | |
- (id)init { | |
_invocations = [[NSMutableArray alloc] init]; | |
return self; | |
} | |
- (void)dealloc { | |
[_proxiedTarget release]; | |
[_invocations release]; | |
[super dealloc]; | |
} | |
#pragma mark Instance methods | |
- (id)prepareWithInvocationTarget:(id)target { | |
[_proxiedTarget autorelease], _proxiedTarget = nil; | |
_proxiedTarget = [target retain]; | |
return self; | |
} | |
- (void)removeAllInvocations { | |
[_invocations removeAllObjects]; | |
} | |
- (NSArray *)operations { | |
NSMutableArray *ops = [NSMutableArray array]; | |
for (NSInvocation *invocation in _invocations) { | |
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invocation]; | |
[ops addObject:operation]; | |
[operation release]; | |
} | |
return [NSArray arrayWithArray:ops]; | |
} | |
- (void)invokeOnQueue:(NSOperationQueue *)queue waitUntilFinished:(BOOL)wait { | |
// Simply avoid -[NSOperationQueue addOperations:waitUntilFinished:] | |
// for iOS 3 compatibility reason | |
for (NSOperation *operation in [self operations]) { | |
[queue addOperation:operation]; | |
} | |
if (wait) { | |
[queue waitUntilAllOperationsAreFinished]; | |
} | |
} | |
- (void)invoke { | |
for (NSInvocation *invocation in _invocations) { | |
[invocation invoke]; | |
} | |
} | |
#pragma mark NSProxy | |
- (void)forwardInvocation:(NSInvocation *)invocation { | |
// Do not invoke now, reserve until `invoke` called. | |
[invocation setTarget:_proxiedTarget]; | |
[_invocations addObject:invocation]; | |
} | |
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel { | |
return [_proxiedTarget methodSignatureForSelector:sel]; | |
} | |
#pragma mark Class method | |
+ (id)invocationManager { | |
return [[[JTInvocationManager alloc] init] autorelease]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment