initWithInvocation:
Returns an
NSInvocationOperation
object initialized with the specified invocation object.
- (id)initWithInvocation:(NSInvocation *)inv
Parameters
- inv
- The invocation object identifying the target object, selector, and parameter objects.
Return Value( NSInvocationOperation initWithInvocation example )
An initialized
NSInvocationOperation
object or nil
if the object could not be initialized.Discussion
This method is the designated initializer. The receiver tells the invocation object to retain its arguments.
( NSInvocationOperation initWithInvocation example )
#import <Foundation/Foundation.h>
@interface Person : NSObject {
NSString *name;
NSUInteger age;
}
- (void)setName:(NSString *)personName age:(NSNumber *)personAge;
@end
@implementation Person
- (void)setName:(NSString *)personName age:(NSNumber *)personAge {
NSLog(@"setName:%@ age:%@", personName, personAge);
name = [personName copy];
age = [personAge unsignedIntegerValue];
}
@end
int main() {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
Person *person = [Person new];
SEL sel = @selector(setName:age:);
NSMethodSignature *sig = [Person instanceMethodSignatureForSelector:sel];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:person];
[inv setSelector:sel];
NSString *name = nil;
NSNumber *age = nil;
[inv setArgument:&name atIndex:2];
[inv setArgument:&age atIndex:3];
// [inv retainArguments];
// [inv invoke];
NSInvocationOperation *op = [[[NSInvocationOperation alloc] initWithInvocation:inv] autorelease];
NSOperationQueue *queue = [NSOperationQueue new];
[queue addOperation:op];
[pool release];
return 0;
}
( NSInvocationOperation initWithInvocation example )
NSInvocationOperation *load = [[NSInvocationOperation alloc] initWithInvocation:loadInvoc];
NSAssert([loadInvoc argumentsRetained],@"Arguments have not been retained");
[loader release];
NSInvocation *completionInvoc = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(serviceCompletionBlock:afterInvocationCompleted:)]];
[completionInvoc setTarget:self];
[completionInvoc setSelector:@selector(serviceCompletionBlock:afterInvocationCompleted:)];
MFEImageCallback callback = [completionBlock copy];
[completionInvoc setArgument:&callback atIndex:2];
[completionInvoc setArgument:&load atIndex:3];
NSInvocationOperation *completion = [[NSInvocationOperation alloc] initWithInvocation:completionInvoc];
NSAssert([completionInvoc argumentsRetained],@"Completion handler not retaining");
[callback release];
[completion addDependency:load];
( NSInvocationOperation initWithInvocation example )
- (void)fetchImageWithURLs:(NSArray *)urlArray {
[self.retriveAvatarQueue cancelAllOperations];
self.retriveAvatarQueue = nil;
NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
for (NSUInteger i=0; i<[urlArray count]; i++) {
NSURL *url = [urlArray objectAtIndex:i];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(cacheImageWithIndex:andURL:)]];
[inv setTarget:self];
[inv setSelector:@selector(cacheImageWithIndex:andURL:)];
[inv setArgument:&i atIndex:2];
[inv setArgument:&url atIndex:3];
NSInvocationOperation *invOp = [[NSInvocationOperation alloc] initWithInvocation:inv];
[opQueue addOperation:invOp];
[invOp release];
}
self.retriveAvatarQueue = opQueue;
[opQueue release];
}
- (void)cacheImageWithIndex:(NSUInteger)index andURL:(NSURL *)url {
NSData *imageData = [NSData dataWithContentsOfURL:url];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = PATH_FOR_IMG_AT_INDEX(index);
NSError *error = nil;
// Save the file
if (![fileManager createFileAtPath:filePath contents:imageData attributes:nil]) {
DLog(@"Error saving file at %@", filePath);
}
// Notifiy the main thread that our file is saved.
[self performSelectorOnMainThread:@selector(imageLoadedAtPath:) withObject:filePath waitUntilDone:NO];
}