Giter Club home page Giter Club logo

each's Introduction

Each

Elegant ⏱ interface for Swift apps

BuddyBuild

Each is a NSTimer bridge library written in Swift.

Features

  • Completely configurable timers
  • Support for time intervals in ms, seconds, minutes and hours
  • Fully extendable
  • More readable and simple to use in comparison with NSTimer object

Requirements

  • iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 8.0+
  • Swift 3.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.1.0+ is required to build Each.

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

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
pod 'Each', '~> 1.2'
end

Then, run the following command:

$ pod install

Carthage

You can use Carthage to install Each by adding it to your Cartfile:

github "dalu93/Each"

Usage

Creating a new timer instance

let timer = Each(1).seconds     // Can be .milliseconds, .seconds, .minute, .hours  

Performing operations

timer.perform {
    // Do your operations
    // This closure has to return a NextStep value
    // Return .continue if you want to leave the timer active, otherwise
    // return .stop to invalidate it
}

If you want to leave the memory management decision to the Each class, you can simply use the perform(on: _) method. It requires that the parameter is an AnyObject instance.

timer.perform(on: self) {
    // Do your operations
    // This closure has to return a NextStep value
    // Return .continue if you want to leave the timer active, otherwise
    // return .stop to invalidate it
}

Stopping the timer manually

timer.stop()    // This stops immediately the timer

Restarting the timer

You can restart the timer only after you stopped it. This method restarts the timer with the same perform closure.

timer.restart()

Leaks

Unfortunately the interface doesn't help you with handling the memory leaks the timer could create. In case of them, two workarounds are provided

Workaround 1

Use the perform(on: _) method as explained in the usage section. Please note that using this method, the timer isn't immediately deallocated when the owner is deallocated. It will be deallocated when the timer triggers the next time and it will check whether the owner instance is still valid or not.

Workaround 2

In case you don't want to declare a property that holds the Each reference, create a normal Each timer in your method scope and return .stop/true whenever the owner instance is nil

Each(1).seconds.perform { [weak self] in
    guard let _ = self else { return .stop }

    print("timer called")
    return .continue
}

90% of closures will call self somehow, so this isn't so bad

Workaround 3

In case the first workaround wasn't enough, you can declare a property that holds the Each reference and call the stop() function whenever the owner is deallocated

final class ViewController: UIViewController {
    private let _timer = Each(1).seconds

    deinit {
        _timer.stop()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        _timer.perform {
            // do something and return. you can check here if the `self` instance is nil as for workaround #1
        }
    }
}

License

Each is released under the MIT license. See LICENSE for details.

each's People

Contributors

dalu93 avatar sjoness avatar stupergenius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

each's Issues

Timer stops during scrolling

If the interface contains any scrollable item (table view, collection view, etc), then timer stops firing events while user scrolls the content.

pod install error

My 'Podfile':

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
target 'XXX' do
  use_frameworks!
  pod 'Each', '~> 1.2'
end

And, pod install
the error is

[!] Unable to satisfy the following requirements:

- `Each (~> 1.2)` required by `Podfile`

None of your spec sources contain a spec satisfying the dependency: `Each (~> 1.2)`.

You have either:
 * out-of-date source repos which you can update with `pod repo update`.
 * mistyped the name or version.
 * not added the source repo that hosts the Podspec to your Podfile.

Note: as of CocoaPods 1.0, `pod repo update` does not happen on `pod install` by default.

but, if I delete pod 'Each', '~> 1.2'
the other pods is available

Memory Leaks

RunLoop retain Each's _timer property cause memory Leaks, Although _timer be marked as weak.

RunLoop -> Timer -> Each

Add `Disposable` concept

To workaround the memory leak issue, an another idea could be to implement the famous pattern of Disposable and Disposer

// MARK: - Disposable declaration
public protocol Disposable {
    func dispose()
}

// MARK: - Disposable
extension Each: Disposable {
    public func dispose() {
        stop()
    }
}

// MARK: - Disposer declaration
public protocol Disposer {
    func add(_ disposable: Disposable)
    func dispose()
}

Make the `perform` closure return type more readable

Actually the return type is Bool

timer.perform {
    // return `true` if you want to stop the timer, otherwise `false` to continue
}

In an implemented way where we want to stop the timer after doing something(), the code below isn't readable by other developers

timer.perform {
    self.something()
    return true    // what does true mean? To continue it or to stop it?
}

A better way could be to declare an enum NextStep (as mentioned by @DeveloperPans) which encapsulates better the return type's meaning

enum NextStep {

    case stop
    case `continue`
}

Which has a fileprivate extension that converts the enum to a Bool for the perform closure.
The final result for the user will be

timer.perform {
    self.something()
    return .stop/.continue
}

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.