tbaranes / SwiftyUtils

All the reusable code that we need in each project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How Timer.every works is not quite intuitive (at least to me)

Sweeper777 opened this issue · comments

I expected Timer.every to fire the timer once immediately after start is called but it actually fires for the first time after the interval specified. Maybe this is just me, but I am thinking whether I should add a default parameter to the every method, something like fireImmediately: Bool = false. If it is set to true, the timer will behave in my "intuitive" way. I made this a default parameter so it will not break any existing code.

Should I make such a change? Do you think this feature should be added?

Sounds good to me. I don't remember why I choose that implementation, but now I think about it, I see no reason to keep it that way. Your solution seems good, and since it has no breaking change, it's even better!

Let's do it 👍

EDIT: Looking back to the code, I'm wondering if it won't be time to improve the API: make it simpler, and closer to the Foundation implementation, since they added it a completion block for iOS10+. That means SwiftyUtils.Timer may be deprecated in 1 or 2 years, for example:

Timer.scheduled(every timeInterval: TimeInterval, delay: TimeInterval = 0, repeat: Bool = true, completion: @escaping (Timer) -> Void)

That's breaking change but can also be a good move.

Fixed in #43