daniele margutti

don't code today what you can't debug tomorrow

The secret world of NSTimer

Tips & tricks of NSTimer and how to create a robust alternative using GCD

This article is also available on Medium at the following link.

Timer allows us to execute some piece of code after a timer interval one or more times.
There are multiple types of clocks used to create timers and, even if apparently all ofthem run at the same rate, they still have different behaviours.

We can list the following list of timer types:

  • Real-time clock or RTC. Its a computer clock (usually in the form of an integrated circuit) that keeps track of the current time; user can change this clock arbitrarily and the NTP (Network Time Protocol) do it best to keep it in sync with an external reference. Its value increases by one second per real second but sometimes it may run faster/slower or jump forward/backward when it attempts to sync with the external source.
  • Monotonic Timer. Its a counter incremented by a physical signal sent to the CPU using an timer interrupt. On Apple platforms this value is returned by the Mach kernel via mach_absolute_time() . Returned value is dependent by the CPU so you can’t just multiply it by a constant to get a real world value; rather, you should call a system-provided conversion function to convert it to a real world value (CoreAnimation has a convenient method for that:  CACurrentMediaTime() ). The fact it gets reset on boot make it not so interesting to get how much time has elapsed in real world, however its the most precise way to measure difference between two intervals.
  • Boot Timer. It’s just a special Monotonic Timer which does not pause when the system goes to sleep; the most common way to get its value is to call the uptime function from terminal.

On Apple platforms the most common way to create timers is by using the NSTimer  class; in fact its just a wrapper around a monotonic timer.
For this reason using NSTimer  may end with an unpredictably behaviour, especially on iOS, where the opportunistic use of resources may ends in some edge cases as described above. Continue reading…

Applications vs Frameworks vs Libraries

As programmers we constantly use frameworks and libraries while developing new applications. Most of you will probably never deal with frameworks or library design nevertheless a certain amount of knowledge of the topic is useful even working on standalone applications.

The following article is an introduction to the differences between these entities and how they interact to produce a consistent and maintainable product.

We will look at their different roles and scopes, when we need one or the other and the best practices for our development cycle.

The last part contains several tips to write a good library I’ve collected in these past years. Continue reading…

UAParserSwift, a lightweight User-Agent parser for Swift Server Side

This is my first contribution to the Swift Server Side community; during these months I’m evaluating Swift as backend language for server side programming. In fact it was not my first attempt, but during this year I’ve seen many improvements in all main server projects like Kitura and Vapor.
Moving away from NodeJS is a tricky decision but the advantages of moving to a modern language like Swift are tempting me since the first release.
So while it’s still a long path to the goal I’m slowly moving to see how I can make this a valuable solution.

With this goal in mind I’ve tried to port a first small contribution to the community by porting of the most famous library used in NodeJS in Swift; UAParserSwift is a port of ua-parser-js made for Swift server side. This library aims to identify detailed type of web browser, layout engine, operating system, cpu architecture, and device type/model, entirely from user-agent string with a relatively small footprint. Continue reading…

Animate UIWindow’s rootViewController transitions

Sometimes during the lifecycle of an application you may need to change the rootViewController  of your main UIWindow ; a typical case maybe the transition between the initial on-boarding and the home of the app (ie. a UITabBarController ).

In order to handle this edge case you may want to create a top controller (typically an UINavigationController  with invisible navigation bar) which enable you to push your new container using a standard push animation.While basically it works fine, the addition of a container used only to handle this single operation is a bit awful to see.

A way better solution is to apply an animated transition (push/pop or slide) to the rootViewController  set so you will get an animate switch from the current view controller to the new one.

Continue reading…