NSTimer and NSRunLoop

My application tracks the user using the CLLocationManager . In the didUpdateToLocation delegate didUpdateToLocation I do the most interesting thing to maintain my position. However, I needed a way to check if they had stopped. This far I could stop recording places and consider their trip. Therefore, I have NSTimer in CCLocationManager , which is added and removed each time didUpdateToLocation called. This will happen when the user stops and the CLLocationManager stops receiving the call.

The only way I could get NSTimer to work:

 [[NSRunLoop mainRunLoop] addTimer:userStoppedMovingTimer forMode:NSRunLoopCommonModes]; 

Then, to remove it:

 [userStoppedMovingTimer invalidate]; 

I have never had to add such timers in the past. Can someone shed some light on why this is so?

+6
source share
1 answer

From the documentation :

There are three ways to create a timer:

  • Use scheduledTimerWithTimeInterval:invocation:repeats: or scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: class the timer creation method and its schedule in the current run loop in default mode.

  • Use timerWithTimeInterval:invocation:repeats: or timerWithTimeInterval:target:selector:userInfo:repeats: class method to create a timer object without scheduling it in a run loop. (After creating it, you must add the timer to the run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.)

  • Highlight a timer and initialize it using initWithFireDate:interval:target:selector:userInfo:repeats: (After creating it, you must add a timer to the startup loop by manually calling the addTimer:forMode: method of the corresponding NSRunLoop object.)

You probably used option 1 earlier, and now you are using option 2 or 3.

+8
source

Source: https://habr.com/ru/post/921356/


All Articles