2010-07-27 7 views
1

나는 내 코드에 문제가있다 나는 NSThread와 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 
+0

문제를 언급하지 않았습니다. 경고/충돌/사물이 실행되지 않았습니까? 문제가 무엇입니까? 또한 왜 당신이 다른 스레드에서'callDectectionMove'를 실행하는지 이해하지 못합니다. 그 메소드에있는 모든 것이 메인 스레드에서 다른 메소드를 실행하고있을 때입니다 ... –

+0

만약 충돌이없고 비슷하지만 if 2 시간 동안 진실하다, 프로그램은 2 개의 다른 실을 점심 먹을 수 없다. 나는 가속도계의 움직임을 인식하고 싶기 때문에이 구조를 만든다. 그리고 나는 3을위한 2 가지 다른 움직임 3을위한 2 개의 다른 스레드를 가져야 만한다 .... ok? – zp26

답변

2

는 [self.arrayMovement 카운트] 스레드의 수는 생성되지만, 모든 스레드가 메인 스레드에서 'detectMove'기능을 실행하려는 때문에 그들은 순차적으로 실행됩니다.

당신은 문 다음 실행하면 :

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

->이 당신이 순차적를 볼 수 있기 때문에 방법 'detectMove가'메인 스레드에서 실행될 수 있도록, 하나 개의 스레드는 한 번에 하나의 문을 실행할 수 있습니다 당신의 스레드 구현에서 작업.

0

결국 detectMove 메서드가 주 스레드에서 실행됩니다.
나는 detectPositionMovement도 메인 스레드에서 실행됩니다 믿습니다.
detectPositionMovement이 완료 될 때까지 어떤 detectMove의 실행도 시작되지 않습니다.

detectPositionMovement (callDectectionMove없이 performSelectorOnMainThread 제외)에서 직접 detectMove으로 전화 해보십시오. 다음 휴식 상기 detectPositionMovement의 점과이 라인이 실행되는 횟수를 확인

0

장소 :

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

그것은 두 개의 스레드가 전달되고 2 번 실행되는 경우. 그렇지 않으면 "if"블록의 조건을 검사합니다.

+0

제 의견으로는 문제가 "첫 번째 istruction이 2 번 실행되었지만 performSelectorOnMainThread no가 배타적이므로"[자가 performSelectorOnMainThread : @selector (detectMove :) withObject : (NSNumber *) arrayIndex waitUntilDone : NO]; " 입니다. 내 문제가 있습니다. – zp26

+0

detectMove 메서드를 두 개의 다른 스레드에서 실행 하시겠습니까? 그렇다면 당신은 주 스레드에서 그것을 exectute해서는 안됩니다. 스레드를 만들지 만 callDetectionMove 메서드 내에서도 주 스레드에서 코드를 실행하기 때문입니다. 결국 실행은 메인 스레드에서 진행됩니다. – Ideveloper

+0

예,하지만 내 프로그램에 대한 스레드가 먼저 performSelectorOnMainThread없이 (내 프로그램을 실행할 수 없습니다) 필요가 실행됩니다. 그리고 이것들은 큰 문제입니다. – zp26