Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop a timer triggered runloop?

if i set up a runloop like that:

NSRunloop* loop = [NSRunloop currentRunLoop];
[runLoop addTimer:anyTimer forMode:NSDefaultRunLoopMode];

can i stop it again ? or is the only way to ignore the notification i use to trigger the further action ?

ok, i give an example for the problem:

-(void)blinkeffekt:(double)pollingTime{

NSRunLoop* runLoop = [NSRunLoop currentRunLoop];

if (pollingTime != 0) {
    NSTimeInterval interval =(double)pollingTime / 1000;
    NSTimer timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(polling) userInfo:nil repeats:YES];
    [runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
}
else {

    [timer invalidate];
}

}

surely, here are several errors - no question. but i think, it shows my problem. and this is not really solvable with the answers until now.

i need to run a timer and stop it later. and ideally out of another function in the class. but then i cannot access "timer" anymore and runloop doesnt allow to figure out, if such a "message" is available. and it would be extremely ineffective, if i would register for each calling of the function a new timer.

like image 662
nico Avatar asked Jul 19 '10 07:07

nico


People also ask

What is run loop mode?

A run loop is an event processing loop that you use to schedule work and coordinate the receipt of incoming events. The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none. Run loop management is not entirely automatic.

What is run loop in iOS?

Overview. A RunLoop object processes input for sources, such as mouse and keyboard events from the window system and Port objects. A RunLoop object also processes Timer events. Your application neither creates nor explicitly manages RunLoop objects.

What is Uikit RunLoop?

Role of a run loop On iOS, a run loop can be attached to a NSThread . Its role is to ensure that its NSThread is busy when there is work to do and at rest when there is none. The main thread automatically launches its run loop at the application launch.


1 Answers

You need to send a invalidate message to the timer to remove it from the RunLoop.

See Doc

[anyTimer invalidate];
like image 191
willcodejavaforfood Avatar answered Oct 13 '22 15:10

willcodejavaforfood