GCDTimer - Well tested Grand Central Dispatch (GCD) Timer in Swift

Overview

Carthage compatible

GCDTimer

Well tested Grand Central Dispatch (GCD) Timer in Swift. Checkout the test file.

Usage

Long running timer

import GCDTimer

class Demo {

  init() {
    let timer = GCDTimer(intervalInSecs: 20)
    
    timer.Event = {
      println("Hello World")
      
      // Send some data to the server
    }
    
    timer.start()
    
    //Don't forget to pause the timer in the AppDelegate:applicationWillResignActive(application: UIApplication) method.
  }

}

Autofinishing timer

import GCDTimer

class Demo {

  init() {
    let timer = GCDTimer(intervalInSecs: 2)
    
    timer.Event = {
      timer.pause()
      //Process after finishing the timer
    }
    
    timer.start()
  }
  
}

Dispatch After - Delay

var index = 0
GCDTimer.delay(2) {
    index += 1
}

Installation

  • Add github "hemantasapkota/GCDTimer" to your cartfile
  • Execute carthage update

Example

The example shows how to use GCDTimer to periodically poll newly submitted items from Hacker News.

ExampleSS

Author

License

The MIT License (MIT)

Copyright (c) 2017 Hemanta Sapkota

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You might also like...
THORChain vault churn countdown timer for macOS tray
THORChain vault churn countdown timer for macOS tray

ChurnCountdown Useful macOS tray app to show THORChain churn countdown. On start

OpenFocusTimer - Pomodoro timer with categories, reflection, history & statistics

OpenFocusTimer Pomodoro timer with categories, reflection, history & statistics.

A complete set of primitives for concurrency and reactive programming on Swift
A complete set of primitives for concurrency and reactive programming on Swift

A complete set of primitives for concurrency and reactive programming on Swift 1.4.0 is the latest and greatest, but only for Swift 4.2 and 5.0 use 1.

Futures is a cross-platform framework for simplifying asynchronous programming, written in Swift.

Futures Futures is a cross-platform framework for simplifying asynchronous programming, written in Swift. It's lightweight, fast, and easy to understa

GroupWork is an easy to use Swift framework that helps you orchestrate your concurrent, asynchronous functions in a clean and organized way
GroupWork is an easy to use Swift framework that helps you orchestrate your concurrent, asynchronous functions in a clean and organized way

GroupWork is an easy to use Swift framework that helps you orchestrate your concurrent, asynchronous functions in a clean and organized way. This help

Hydra ⚡️ Lightweight full-featured Promises, Async & Await Library in Swift
Hydra ⚡️ Lightweight full-featured Promises, Async & Await Library in Swift

Lightweight full-featured Promises, Async & Await Library in Swift What's this? Hydra is full-featured lightweight library which allows you to write b

Kommander is a Swift library to manage the task execution in different threads.
Kommander is a Swift library to manage the task execution in different threads.

A lightweight, pure-Swift library for manage the task execution in different threads. Through the definition a simple but powerful concept, Kommand.

SwiftCoroutine - Swift coroutines for iOS, macOS and Linux.
SwiftCoroutine - Swift coroutines for iOS, macOS and Linux.

Many languages, such as Kotlin, Go, JavaScript, Python, Rust, C#, C++ and others, already have coroutines support that makes the async/await pattern i

Venice - Coroutines, structured concurrency and CSP for Swift on macOS and Linux.
Venice - Coroutines, structured concurrency and CSP for Swift on macOS and Linux.

Venice provides structured concurrency and CSP for Swift. Features Coroutines Coroutine cancelation Coroutine groups Channels Receive-only chan

Comments
  • Retain cycle

    Retain cycle

    timerSource retains self while self retains timerSource.

    private let timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, GCDTimer.gcdTimerQueue)
    
    dispatch_source_set_event_handler(timerSource, { () -> Void in
                    self.event()
     })
    

    Something like this should break retain cycle:

    dispatch_source_set_event_handler(timerSource, { [weak self] () -> Void in
            self?._event()
    })
    
    opened by S2Ler 1
  • Better Installation Section

    Better Installation Section

    Hey, your library is really interesting. I created this iOS Open source Readme Template so you can take a look on how make your Installation Section better If you want, I can help you to organize the lib.

    What are your thoughts? 😄

    opened by lfarah 0
Owner
Hemant Sapkota
Software Architect. React Native, React, Golang, Swift, Objective-C, Android, Java, Kotlin, AWS - https://codeplayground.app
Hemant Sapkota
Grand Central Dispatch simplified with swift.

GCDKit GCDKit is Grand Central Dispatch simplified with Swift. for Swift 1.2: Use version 1.0.1 for Swift 2.1 / 2.2: Use the master branch Introductio

John Estropia 317 Dec 6, 2022
A wrapper of Grand Central Dispatch written in Swift

GCD A wrapper of Grand Central Dispatch written in Swift. Examples gcd // submit your code for asynchronous execution on a global queue with high prio

Le Van Nghia 75 May 19, 2022
Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch (iOS7+ and OS X 10.9+ compatible)

Async.legacy Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch (GCD) Async rewritten for iOS7 and OS X 10.9 Compatibility

Joseph Lord 31 Jul 1, 2019
Chronos is a collection of useful Grand Central Dispatch utilities

Chronos is a collection of useful Grand Central Dispatch utilities. If you have any specific requests or ideas for new utilities, don't hesitate to create a new issue.

Comyar Zaheri 248 Sep 19, 2022
Queuer is a queue manager, built on top of OperationQueue and Dispatch (aka GCD).

Queuer is a queue manager, built on top of OperationQueue and Dispatch (aka GCD). It allows you to create any asynchronous and synchronous task easily, all managed by a queue, with just a few lines.

Fabrizio Brancati 1k Dec 2, 2022
AsyncTimer is a precision asynchronous timer. You can also use it as a countdown timer

AsyncTimer ?? Features Can work as a countdown timer Can work as a periodic Timer Can work as a scheduled timer Working with user events (like: scroll

Adrian Bobrowski 26 Jan 1, 2023
Plugin-spell-timer - Spell Timer Plugin for Outlander

Spell Timer Plugin for Outlander This plugin provides variables for spells from

Outlander 0 Jan 22, 2022
Schedule timing task in Swift using a fluent API. (A friendly alternative to Timer)

Schedule(简体中文) Schedule is a timing tasks scheduler written in Swift. It allows you run timing tasks with elegant and intuitive syntax. Features Elega

Luo Xiu 1.8k Jan 7, 2023
Egg-Timer - Intermediate Swift Programming - Control Flow and Optionals

Egg-Timer Intermediate Swift Programming - Control Flow and Optionals What I lea

null 0 Jan 10, 2022
Egg Timer app helps you to cook your egg in the way you want

Egg Timer Egg Timer app helps you to cook your egg in the way you want. You need to decide on how do you want to eat your egg than just click the egg

Emrullah Cirit 2 Nov 29, 2022