views:

52

answers:

3

Hi, 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
+2  A: 

In your code [self.arrayMovement count] number of threads will be created, but they will run in a sequential order since all thread want to execute 'detectMove' function in main thread.

When you execute following statement:

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

-> make method 'detectMove' to be executed in Main thread, one thread can execute only one statement at a time, because of this you will see a sequential operation from you thread implementation.

Girish Kolari
A: 

Eventually, the detectMove method is executed in the main thread.
I believe that detectPositionMovement is also executed from the main thread.
So the execution of any detectMove won't start until the detectPositionMovement is completed.

Try to call detectMove directly from detectPositionMovement (without callDectectionMove and without performSelectorOnMainThread).

Michael Kessler
A: 

Place the break point in the detectPositionMovement and then check how many times this line is being executed:

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

if it is being executed 2 times then two thread are being dispatched. if not then checks the conditions in "if" block.

Ideveloper
in my opinion the problem is the " [self performSelectorOnMainThread:@selector(detectMove:) withObject:(NSNumber*)arrayIndex waitUntilDone:NO]; "because the first istruction was execute 2 times but the performSelectorOnMainThread no because is exclusive.My problem is here.
zp26
do you want to execute your detectMove Method in two different threads?. If yes then you should not exectute it on the main thread. Because even if you are creating threads but inside callDetectionMove method you are executing the code on the main thread. So finally the execution is going on the main thread.
Ideveloper
yes, but for my program is necessary that the threads are execute for first(without performSelectorOnMainThread my program can't execute these). And these is a big problem.
zp26