views:

111

answers:

2

Hi I have a problem in my code. I want to launch the thread but when it is launched they can't start a NSTimer istruction. Can you help me?

-(void)detectionMove:(NSTimer*)timer{

    int indexArray = [[[timer userInfo] objectForKey:@"arrayIndex"] intValue];
    // do something
}



-(void)callDectectionMove:(NSNumber*)arrayIndex{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 

    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];  
    [myDictionary setObject:arrayIndex forKey:@"arrayIndex"];  

    //This istruction can't lunch a detectionMove method
    [NSTimer scheduledTimerWithTimeInterval:timeToCatch target:self selector:@selector(detectionMove:) userInfo:myDictionary repeats: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])
            [NSThread detachNewThreadSelector:@selector(callDectectionMove:) toTarget:self withObject:[NSNumber numberWithInt:(int)i]]; 

        }
}
A: 

You are creating NSTimer in the threat and timer object is added to pool(since it is class method), so before your timer is getting called the timer object will be released(since thread context will be over before timer is fired).

There are 2 solution 1. Add a retain count to the NSTimer object and release once you done with timer work. 2. Make sure your thread is not exited until timer is done its work.

Girish Kolari
Not quite correct. A scheduled Timer is implicitly retained. There's no need to retain it. Your second thought maybe right tough.
tonklon
it is the concept object is added to current auto release pool before object is returned from class method .in you case object 'pool' is the current auto release pool which is release before thread is exited. so when 'pool' is released all object inside it will get release message - so to retain the object you should increase the retain count by 1.
Girish Kolari
I did a detail study using a sample program, some of the observations1. Timer fire on the same run loop where it created, every thread have their own run loop so your thread should be running to make your timer fire on the time you have mentioned.2. scheduledTimerWithTimeInterval return NSTimer object with retain count as '2' so where one count will be reduced when auto release pool is released and second one will be reduced once the timer event is fired.
Girish Kolari
A: 

Do you really, really need a different Thread? You want to start a timer on that background Thread, why can't you start the timer on the main thread?

To answer your question: The fist problem is: Your Thread does not run long enough like Girish suggested.

The second problem is: You are not looping the Thread's runloop. A runloop is needed for a timer to work.

See also:

http://stackoverflow.com/questions/2083737/running-nstimer-within-an-nsthread

http://stackoverflow.com/questions/3156452/threaded-nstimer

tonklon