If NSTimer is always added to runloop to execute

I do not add timers to start loops, and it works fine. The other day, I read an article about NSRunLoopand said that it is better to add an instance NSTimerto the runloop instance for execution. I'm just wondering if he would be harmed if I didn’t.

+4
source share
2 answers

NSTimerinstances must always be scheduled at runtime for proper operation. If you do this from the main thread, you can just use it scheduleTimerWithTimeInterval, and it will be automatically added to the main loop of the loop for you, and a manual method call is NSRunLoop addTimernot required. But you can create a timer and add it yourself if you want. scheduleTimerWithTimeInterval- A convenience method that just does it for you.


If you create a timer from a background thread that does not have its own run loop (and by default when using background dispatch queues or operation queues, the thread that is running will not have its own loop run), you must manually add the timer to the run loop. Typically, people simply add a timer to the main startup loop.

, , , , GCD, . . fooobar.com/questions/958131/... Objective-C. . fooobar.com/questions/83124/... Swift.

, , scheduledTimerWithTimeInterval, , .

+6

, , . , runloop.

timerWithTimeInterval:invocation:repeats:
timerWithTimeInterval:target:selector:userInfo:repeats

runloop runloop , . .

scheduledTimerWithTimeInterval:invocation:repeats:
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
0

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


All Articles