2010-02-24 9 views
15

Me gustaría hacer un subproceso con múltiples argumentos. ¿Es posible? tengo la función:selector de llamadas con dos argumentos en NSThread problema

 
-(void) loginWithUser:(NSString *) user password:(NSString *) password { 
} 

Y quiero llamar a esta función como un selector:

 

[NSThread detachNewThreadSelector:@selector(loginWithUser:user:password:) toTarget:self withObject:@"someusername" withObject:@"somepassword"]; // this is wrong 


cómo pasar dos argumentos de parámetro withObject sobre esta función detachNewThreadSelect?

¿Es posible?

Respuesta

16

lo necesario para pasar los parámetros adicionales en un objeto pasado a withObject así:

NSDictionary *extraParams = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"user",@"password",nil] andKeys:[NSArray arrayWithObjects:@"valueForUser",@"valueForPassword",nil]] 

[NSThread detachNewThreadSelector:@selector(loginWithUser:) toTarget:self withObject:extraParams]; 
+0

Gracias por una gran respuesta a esta pregunta @Lance. – iPadDeveloper2011

+0

Olvidó el punto y coma al final de su primera línea ;-) – Ken

0

envolver su método seleccionado con un método de envolver auxiliar, "wrappingMethod", que procesa una NSArray de insumos para adaptarse a su método propio antes de llamar a su propio método dentro de wrappingMethod. Ahora separe un NSThread que selecciona su nuevo wrappingMethod y toma la instancia de NSArray para withObject.

Aparte: tener un contenedor aquí puede ser particularmente útil si su método original toma uno o más tipos primitivos como entrada y luego tiene que trabajar con NSNumber o NSStrings, por ejemplo, para satisfacer NSThread.

6

Esta es la parte superior de mi cabeza, no probado:

NSThread+ManyObjects.h:

@interface NSThread (ManyObjects) 

+ (void)detachNewThreadSelector:(SEL)aSelector 
         toTarget:(id)aTarget 
        withObject:(id)anArgument 
         andObject:(id)anotherArgument; 

@end 

NSThread+ManyObjects.m:

@implementation NSThread (ManyObjects) 

+ (void)detachNewThreadSelector:(SEL)aSelector 
         toTarget:(id)aTarget 
        withObject:(id)anArgument 
         andObject:(id)anotherArgument 
{ 
    NSMethodSignature *signature = [aTarget methodSignatureForSelector:aSelector]; 
    if (!signature) return; 

    NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature]; 
    [invocation setTarget:aTarget]; 
    [invocation setSelector:aSelector]; 
    [invocation setArgument:&anArgument atIndex:2]; 
    [invocation setArgument:&anotherArgument atIndex:3]; 
    [invocation retainArguments]; 

    [self detachNewThreadSelector:@selector(invoke) toTarget:invocation withObject:nil]; 
} 

@end 

y luego se puede importar NSThread+ManyObjects y llame

[NSThread detachNewThreadSelector:@selector(loginWithUser:user:password:) toTarget:self withObject:@"someusername" andObject:@"somepassword"]; 
0

Una actualización de respuesta agradable de ennuikiller:

NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:@"IMG_URL_VALUE",@"img_url",@"PARAM2_VALUE", @"param2", nil]; 

[NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:params]; 
Cuestiones relacionadas