Reputation: 1527
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
Upvotes: 1
Views: 397
Reputation: 1463
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.
Upvotes: 0
Reputation: 14235
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
).
Upvotes: 0
Reputation: 2515
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.
Upvotes: 2