ios - Should a NSTimer always be added into a runloop to execute -


i don't explicitly add timers run loops , works fine. other day when read article nsrunloop , said it's better add nstimer instance runloop instance execute. wonder harm if don't so?

nstimer instances need scheduled on run loop operate properly. if you're doing main thread, can use scheduletimerwithtimeinterval , automatically added main run loop , no manual call nsrunloop method addtimer needed. can create timer , add yourself, if want. scheduletimerwithtimeinterval convenience method you.


if creating timer background thread doesn't have own run loop (and default, when use background dispatch queues or operation queues, thread on running not have own run loop), have manually add timer run loop. typically, people add timer main run loop.

alternatively, if want timer run on background thread, rather creating run loop thread , adding timer new run loop, can use gcd dispatch timers, don't require run loop run. see https://stackoverflow.com/a/19996367/1271826 objective-c example. see https://stackoverflow.com/a/25952724/1271826 swift example.

so, unless creating timers in background thread, use scheduledtimerwithtimeinterval, , don't have worry manually adding run loop.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -