У меня есть проблема с моим кодом, я хочу создать 2 других экземпляра с NSThread, но я думаю в своей проблеме, их не происходит. Можно ли взять мой решение для моей проблемы. Я отправляю свой код, если Вы можете, можно ли показать пример решения?Спасибо
@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
Поместите точку останова в detectPositionMovement и затем проверьте, сколько раз эта строка выполняется:
[NSThread detachNewThreadSelector:@selector(callDectectionMove:) toTarget:self withObject:[NSNumber numberWithInt:(int)i]];
если она выполняется 2 раза, то отправляются два потока. если нет, то проверяет условия в блоке «если».
В конце концов, метод detectMove
выполняется в основном потоке.
Я считаю, что detectPositionMovement
также выполняется из основного потока.
Таким образом, выполнение любого detectMove
не начнется, пока не будет завершено выполнение detectPositionMovement
.
Попробуйте вызвать detectMove
непосредственно из detectPositionMovement
(без callDectectionMove
и без performSelectorOnMainThread
).
В вашем коде [self.arrayMovement count] будет создано количество потоков, но они будут выполняться в последовательном порядке, поскольку все потоки хотят выполнить функцию «detectMove» в основном потоке.
Когда вы выполняете следующий оператор:
[self performSelectorOnMainThread:@selector(detectMove:) withObject:(NSNumber*)arrayIndex waitUntilDone:NO];
-> сделать метод «detectMove» для выполнения в основном потоке, один поток может выполнять только один оператор за раз, из-за этого вы увидите последовательную операцию от реализации вашего потока .