40

When I call scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: on the main thread and set time interval to 5 seconds code below timer gets executed, and after 5 seconds timer selector is called.

But if I try same in some background thread, the code below scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: will not be executed, it will wait for the timer to fire and then gets executed. Of course, in order to run the timer in the background thread, I first got an instance of NSRunLoop and run it.

Is there a way to set the timer in the background thread and make it non-blocking, so code after it gets executed immediately?

p u
  • 1,395
  • 1
  • 17
  • 30
MegaManX
  • 8,766
  • 12
  • 51
  • 83
  • 1
    It is recommended by apple that NSTimer be run in the main thread. In the background thread time taken to fire NSTimer may be longer than expected. NSTimer is non-blocking by default. It doesn't stop execution of the lines written after it. –  Oct 09 '12 at 12:31
  • @iSaalis can you please share the corresponding apple-doc link. – Shad Jun 15 '17 at 08:44
  • does NSTimer get run in the main thread by default or each NSTimer gets a new thread ? – isJulian00 Mar 21 '19 at 23:14
  • @izzyMachado It's something like Every time you launch your app on iOS, the system creates a Thread — the main thread. Each Thread has a RunLoop automatically created for it as needed.Currently, each Timer fires on the main thread and is attached to a RunLoop. – Diksha235 Mar 25 '19 at 04:57

1 Answers1

11

NSTimer requires an active run loop, when initialized in Main Thread it automatically uses the main run loop. If you need to make a background timer you need attach it to the thread’s run loop and invoke run() to make it active.

  1. NSTimer needs one live NSRunLoop to execute it’s events. In main thread, the NSRunLoop is always live and will never stop until the app is terminated, but in other threads, you must invoke run() to active the NSRunLoop.

  2. NSTimer must invoke invalidate() to release the current timer, otherwise, the timer will retain a strong reference of the current instance of target, and it will remain in memory until invalidate() invoked or app terminated;

  3. NSTimer must created and invalidated in the same thread, and a lot of times, we may forget that.

Take a look at this example , it may be helpful >> http://www.acttos.org/2016/08/NSTimer-and-GCD-Timer-in-iOS/ and the documentation : https://developer.apple.com/documentation/foundation/nstimer

Diksha235
  • 442
  • 7
  • 17
  • does NSTimer get run in the main thread by default or each NSTimer gets a new thread ? – isJulian00 Mar 21 '19 at 23:14
  • 1
    @izzyMachado It's something like Every time you launch your app on iOS, the system creates a Thread — the main thread. Each Thread has a RunLoop automatically created for it as needed.Currently, each Timer fires on the main thread and is attached to a RunLoop. – Diksha235 Mar 25 '19 at 04:56
  • 1
    do you know if we have a timer that fires the scheduled method after 60 seconds but the app goes to the background before the 60 seconds is up, does that method still get called or does it get paused and the the countdown to 60 seconds continue where it left off ? – isJulian00 Mar 25 '19 at 19:02
  • @izzyMachado When in the background, this timer obviously doesn't fire i.e that method will not get called the app will enter in the background state. Check this Robust Way to do so - https://stackoverflow.com/questions/8415870/scheduled-nstimer-when-app-is-in-background – Diksha235 Mar 26 '19 at 06:15