Reputation: 310
I'm using NSOperationQueue in my app and i want to set multiples arguments to my operation how can i do that?
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(methodCall) object:nil];
[queue addOperation:operation];
[operation release];
Upvotes: 0
Views: 2152
Reputation: 36
//Correct approach is to use NSInvocation
//create nsinvocation obj
SEL selector= @selector(methodName:);
NSMethodSignature * sig= [[self class] instanceMethodSignatureForSelector: selector];
NSInvocation * invocation=[NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget: self];
[invocation setSelector:selector];
[invocation setArgument:&firstArgument atIndex: 2];
[invocation setArgument:&secArgument atIndex: 3];
//operation with invocation
NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithInvocation:invocation];
[opQueue addOperation:operation];
Upvotes: 2
Reputation: 1283
You will have to create an array or dictionary with the data you need.
Ex:
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
NSDictionary *argumentDictionary = [NSDictionary dictionaryWithObjectsAndKeys:object1, @"Object1Key", object2, @"Object2Key", nil];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(methodCall:) object:argumentDictionary];
[queue addOperation:operation];
[operation release];
and in - (void)methodCall:(NSDictionary *)argumentDictionary
you can use the objects and values stored in that dictionary.
Upvotes: 6