1

I have a problem with my code I wanna create 2 different instance with NSThread but i think in my problem these doesn't happen. Can you take my a solution for my problem. I post my code, if you can you may show a solution example? Thanks

@implementation myClass


-(void)detectMove:(NSNumber*)arrayIndex{

    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];  
    [myDictionary setObject:arrayIndex forKey:@"arrayIndex"];  

    identificationMove *identifier = [[identificationMove alloc]init];
    [identifier setArrayIndex:(NSNumber*)arrayIndex];
    [identifier detectionMove];

    [identifier release];

}


-(void)callDectectionMove:(NSNumber*)arrayIndex{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  

    [self performSelectorOnMainThread:@selector(detectMove:) withObject:(NSNumber*)arrayIndex waitUntilDone:NO];  

    [pool release];
}


-(void)detectPositionMovement{


    for(int i = 0; i< [self.arrayMovement count]; i++){

        if((actualAccelerometerX+sensibilityMovement) > [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueX] && (actualAccelerometerX-sensibilityMovement) < [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueX] &&
           (actualAccelerometerY+sensibilityMovement) > [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueY] && (actualAccelerometerY-sensibilityMovement) < [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueY] &&
           (actualAccelerometerZ+sensibilityMovement) > [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueZ] && (actualAccelerometerZ-sensibilityMovement) < [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueZ])

                    //I'm not sure that these istruction can start a 2 different and indipendent thread
            [NSThread detachNewThreadSelector:@selector(callDectectionMove:) toTarget:self withObject:[NSNumber numberWithInt:(int)i]];  

        }
}

@end
4

3 回答 3

2

在您的代码中 [self.arrayMovement count] 将创建线程数,但它们将按顺序运行,因为所有线程都希望在主线程中执行“detectMove”函数。

当您执行以下语句时:

[self performSelectorOnMainThread:@selector(detectMove:) withObject:(NSNumber*)arrayIndex waitUntilDone:NO];

-> 使方法“detectMove”在主线程中执行,一个线程一次只能执行一条语句,因此您将看到线程实现的顺序操作。

于 2010-07-27T11:05:37.803 回答
0

最终,该detectMove方法在主线程中执行。
我相信这detectPositionMovement也是从主线程执行的。
所以 any 的执行detectMove在完成之前不会开始detectPositionMovement

尝试detectMove直接从detectPositionMovement(没有callDectectionMove和没有performSelectorOnMainThread)调用。

于 2010-07-27T11:12:21.097 回答
0

将断点放在 detectPositionMovement 中,然后检查该行执行了多少次:

[NSThread detachNewThreadSelector:@selector(callDectectionMove:) toTarget:self withObject:[NSNumber numberWithInt:(int)i]];

如果它被执行 2 次,那么将分派两个线程。如果不是,则检查“if”块中的条件。

于 2010-07-27T11:55:37.250 回答