1

コードに問題があり、NSThread で 2 つの異なるインスタンスを作成したいのですが、私の問題ではこれらは起こらないと思います。私の問題の解決策を教えてもらえますか。コードを投稿します。解決策の例を示していただけますか? ありがとう

@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' をメイン スレッドで実行するようにします。1 つのスレッドは一度に 1 つのステートメントしか実行できません。このため、スレッド実装からの順次操作が表示されます。

于 2010-07-27T11:05:37.803 に答える
0

detectPositionMovement にブレーク ポイントを配置し、この行が実行されている回数を確認します。

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

2回実行されている場合、2つのスレッドがディスパッチされています。そうでない場合は、「if」ブロックの条件をチェックします。

于 2010-07-27T11:55:37.250 に答える
0

最終的に、detectMoveメソッドはメインスレッドで実行されます。メインスレッドからも実行される
と思います。そのため、が完了するまで 実行は開始されません。detectPositionMovement
detectMovedetectPositionMovement

detectMoveから直接呼び出してみてくださいdetectPositionMovement(なしcallDectectionMoveとなしperformSelectorOnMainThread)。

于 2010-07-27T11:12:21.097 に答える