Code Monkey home page Code Monkey logo

repeat's Introduction

Repeat: modern NSTimer in GCD

Version License Platform CocoaPods Compatible Carthage Compatible Twitter

★★ Star me to follow the project! ★★

Repeat is small lightweight alternative to NSTimer with a modern Swift Syntax, no strong references, multiple observers reusable instances. Repeat is based upon GCD - Grand Central Dispatch.

› Learn More: If you want to learn more about it check out my article on Medium.

Main features offered by Repeat are:

  • Simple, less verbose APIs methods to create and manage our timer. Just call every() or once to create a new Timer even in background thread.
  • Avoid strong reference to the destination target and avoid NSObject inheritance.
  • Support multiple observers to receive fire events from timer.
  • Ability to pause , start , resume and reset our timer without allocating a new instance.
  • Ability to set different repeat modes (infinite : infinite sequence of fires, at regular intervals, finite : a finite sequence of fires, at regular intervals, once : a single fire events at specified interval since start).

Examples

Create single fire timer

The following code create a timer which fires a single time after 5 seconds.

Repeat.once(after: .seconds(5)) { timer in
  // do something	
}

Create recurrent finite timer

The following code create a recurrent timer: it will fire every 10 minutes for 5 times, then stops.

Repeat.every(.minutes(10), count: 5) { timer  in
  // do something		
}

Create recurrent infinite timer

The following code create a recurrent timer which fires every hour until it is manually stopped .

Repeat.every(.hours(1)) { timer in
  // do something
}

Manage a timer

You can create a new instance of timer and start as needed by calling the start() function.

let timer = Repeat(interval: .seconds(5), mode: .infinite) { _ in
	// do something		
}
timer.start()

Other functions are:

  • start(): start a paused or newly created timer
  • pause(): pause a running timer
  • reset(_ interval: Interval): reset a running timer, change the interval and restart again
  • fire(): manually fire an event of the timer from an external source

Adding/Removing Observers

By default a new timer has a single observer specified by the init functions. You can, however, create additional observer by using observe() function. The result of this call is a token identifier you can use to remove the observer in a second time. Timer instance received in callback is weak.

let token = timer.observe { _ in
	// a new observer is called		
}
timer.start()

You can remove an observer by using the token:

timer.remove(token)

Requirements

Repeat is compatible with Swift 4.x. All Apple platforms are supported:

  • iOS 8.0+
  • macOS 10.9+
  • watchOS 2.0+
  • tvOS 9.0+

Installation

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like Repeat in your projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.0.1+ is required to build Repeat.

Install via Podfile

To integrate Repeat into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

target 'TargetName' do
use_frameworks!
pod 'Repeat'
end

Then, run the following command:

$ pod install

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Repeat into your Xcode project using Carthage, specify it in your Cartfile:

github "malcommac/Repeat"

Run carthage to build the framework and drag the built Repeat.framework into your Xcode project.

repeat's People

Contributors

malcommac avatar

Watchers

James Cloos avatar Egor avatar

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.