Code Monkey home page Code Monkey logo

pdktimer's Introduction

PDKTimer

A simple swift GCD based Timer

Why?

Why do we need a new timer if we already have NSTimer?

NSTimer is an Objective-C class that needs a @selector to call. As in swift, we don't have selectors, whe have to pass a String with the name of the function we want to be called. Something like :

let timer = NSTimer(timeInterval: 0.5, target: self, selector: "myfunction", userInfo: nil, repeats: false)

Yikes!

The problem with that is that myfunction string will be evaluated in run-time. That means we would make a typo in the name of the function and the compiler won't be able to tell us that code won't execute.

Wouldn't it be nice if whe could pass a closure to the timer?

How to use it

Using PDKTimer is really easy. Simply call

let timer = PDKTimer(timeInterval: 5.0, repeats: true){
	// do something
}

and that closure will be executed each 5 seconds

Note (Scheduling):

PDKTimer instances don't do auto schedule. You have to manually call schedule in order to start.

let timer = PDKTimer(timeInterval: 5.0, repeats: true){
	// do something
}
timer.schedule()

GCD powered

PDKTimer is implemented using GCD APIs. It isn't a Swift wrapper that uses NSTimer on the inside. This allows not only executing the timer in a background queue, but dispatching the timed closure on a specific dispatch queue. Simply create your timer using :

let dispatchQueue = dispatch_queue_create("com.produkt.pdktimer.test", DISPATCH_QUEUE_SERIAL)
PDKTimer(timeInterval: 0.5, repeats: false, dispatchQueue: dispatchQueue){
	// do something
}

and your closure will be executed on your custom queue

Syntax sugar

I really liked SwiftyTimer API from radex, that is a Swift wrapper for NSTimer So I adopted the API that he suggests.

You can create repeating and non-repeating timers with PDKTimer.every and PDKTimer.after short-hand initializers

PDKTimer.every(5.seconds){
	// executes every 5 seconds
}

PDKTimer.after(5.seconds){
    // waits 5 seconds and then executes once
}

An extension of Double is also defined so you can define time intervals with Ruby-on-Rails-like helpers

500.milliseconds
1.second
2.5.seconds
5.seconds
10.minutes
1.hour

Note (Scheduling):

PDKTimer short-hand initializers do auto schedule. So you don't need to manually call schedule

PDKTimer.every(5.seconds){

}
// no scheduling needed

Author

Daniel García

https://github.com/fillito https://github.com/Produkt

Thanks

Thanks to Javi Soto for writing MSWeakTimer. It helped me a lot understanding som GCD APIs to implement a timer

Thanks to Radek Pietruszewski for designing SwiftyTimer API, that I basically ripped off

pdktimer's People

Contributors

fillito avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

curiosity

pdktimer's Issues

Swift 3?

Any plans to migrate PDKTimer to Swift 3?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.