Which one to use? NSTimer or timer dispatch_source?

Well, as I said in my previous post, Apple people don’t maintain AppKit framework as they did before.

So, as a developer you would need to decide functions from which framework to use. What I means is, for example when Apple introduced a feature in Core Graphics, a corresponding NS* class and its messages used the newly implemented feature in Core Graphics. So, you don’t really need to access Core Graphics directly except for some special cases where you need to access those lower level functions than AppKit directly.

However, now you will decide which one to use more frequently than you did so before.
For example, scheduledTimerWithTimeInterval:invocation:repeats of NSTimer doesn’t look to use the timer dispatch_source. if it uses timer dispatch_source internally, you will not need to worry about which one to use. But now you should do so.

There can be some reason you would prefer using NSTimer message.

  1. You have old source code which was written for OS X earlier than OS X 10.6 Snow Leopard and want to keep the source code
  2. You want to ensure source code level compatibility with older OS X. ( Well, you would say the version of Xcode required to build for the older OS is not installable on current Mac OS X. But if your company is serious about supporting older but still decent OS X, they should keep the dev. environment for you.
  3. You want to ensure source code level compatibility with GNUStep. ( I wonder how many people/companies actually care about GNUStep for commercial S/W. )
  4. You prefer the simple NSTimer use pattern.

However, you would prefer using the timer dispatch_source if you want :

  1. Better performance  and caring of multi-core architecture environment more well presumably
    • NSTimer is scheduled in Run-Loop, while dispatch_source is in GCD space. So, inherently dispatch_source will utilize multicore architecture more well, although it could be negligible practically.
  2. higher timer resolution ( NSTimer is in seconds, while dispatch_source is in nanosecond )
    • Probably this is the strongest reason you want to use dispatch_source.

Any other reason to prefer one to the other?


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: