Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

performSelectorInBackground with multiple params

How can I call a method with multiple params like below with performSelectorInBackground?

Sample method:

-(void) reloadPage:(NSInteger)pageIndex firstCase:(BOOL)firstCase;
like image 917
ysnky Avatar asked Mar 14 '11 14:03

ysnky


3 Answers

The problem is that performSelectorInBackground:withObject: takes only one object argument. One way to get around this limitation is to pass a dictionary (or array) of arguments to a "wrapper" method that deconstructs the arguments and calls your actual method:

- (void)callingMethod {
    NSDictionary * args = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithInteger:pageIndex], @"pageIndex",
                            [NSNumber numberWithBool:firstCase], @"firstCase",
                            nil];
    [self performSelectorInBackground:@selector(reloadPageWrapper:)
                           withObject:args];
}

- (void)reloadPageWrapper:(NSDictionary *)args {
    [self reloadPage:[[args objectForKey:@"pageIndex"] integerValue]
           firstCase:[[args objectForKey:@"firstCase"] boolValue]];
}

- (void)reloadPage:(NSInteger)pageIndex firstCase:(BOOL)firstCase {
    // Your code here...
}

This way you're only passing a "single" argument to the backgrounding call, but that method can construct the multiple arguments you need for the real call (which will take place on the same backgrounded thread).

like image 108
Tim Avatar answered Nov 07 '22 15:11

Tim


I've just found this question and wasn't happy with any of the answers. In my opinion neither make good use of the tools available, and passing around arbitrary information in arrays and dictionaries generally worries me.

So, I went and wrote a small NSObject category that will invoke an arbitrary selector with a variable number of arguments:

Category Header

@interface NSObject (NxAdditions)

-(void)performSelectorInBackground:(SEL)selector withObjects:(id)object, ... NS_REQUIRES_NIL_TERMINATION;

@end

Category Implementation

@implementation NSObject (NxAdditions)

-(void)performSelectorInBackground:(SEL)selector withObjects:(id)object, ...
{
    NSMethodSignature *signature = [self methodSignatureForSelector:selector];

    // Setup the invocation
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    invocation.target = self;
    invocation.selector = selector;

    // Associate the arguments
    va_list objects;
    va_start(objects, object);
    unsigned int objectCounter = 2;
    for (id obj = object; obj != nil; obj = va_arg(objects, id))
    {
        [invocation setArgument:&obj atIndex:objectCounter++];
    }
    va_end(objects);

    // Make sure to invoke on a background queue
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invocation];
    NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];
    [backgroundQueue addOperation:operation];
}

@end

Usage

-(void)backgroundMethodWithAString:(NSString *)someString array:(NSArray *)array andDictionary:(NSDictionary *)dict
{
    NSLog(@"String: %@", someString);
    NSLog(@"Array: %@", array);
    NSLog(@"Dict: %@", dict);
}

-(void)someOtherMethod
{
    NSString *str = @"Hello world";
    NSArray *arr = @[@(1337), @(42)];
    NSDictionary *dict = @{@"site" : @"Stack Overflow",
                           @"url" : [NSURL URLWithString:@"http://stackoverflow.com"]};

    [self performSelectorInBackground:@selector(backgroundMethodWithAString:array:andDictionary:)
                          withObjects:str, arr, dict, nil];
}
like image 6
Steve Wilford Avatar answered Nov 07 '22 14:11

Steve Wilford


Well, I have used this:

[self performSelectorInBackground:@selector(reloadPage:)
                       withObject:[NSArray arrayWithObjects:pageIndex,firstCase,nil] ];

for this:

- (void) reloadPage: (NSArray *) args {
    NSString *pageIndex = [args objectAtIndex:0];    
    NSString *firstCase = [args objectAtIndex:1];    
}
like image 5
Roozbeh Zabihollahi Avatar answered Nov 07 '22 15:11

Roozbeh Zabihollahi