SwiftyDispatch 0.3.0

SwiftyDispatch 0.3.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2015
SPMSupports SPM

Maintained by Francis Chong.



SwiftyDispatch

A lightweight GCD wrapper for Swift. Mostly copied from MacRuby’s Dispatch module.

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Queue

Create queues

// Create a serial queue
let queue = Queue("Serial", .Serial)

// Create a concurrent queue
let queue = Queue("Concurrent", .Concurrent)

// It is serial by default
let queue = Queue("Concurrent")

// Get main queue
let queue = Queue.main()

// Get global queue via QOS
let queue = Queue.concurrent(.UserInteractive)

// Get global queue via priority
let queue = Queue.concurrent(priority: .Background)

Run blocks

// asynchronously
queue.async {
  data = true
}

// synchronously
queue.sync {
  data = true
}

Apply

queue.apply(10) {
  // some concurrent task
}

After

queue.apply(0.3) {
  // run after 0.3 seconds
}

Semaphore

Semaphore provides an efficient mechanism to synchronizes threads via a combination of waiting and signaling.

This is especially useful for controlling access to limited resources.

let queue = Queue("sample")
let semaphore = Semaphore(0)
queue.after(1.0) {
  print "Hello"
  semaphore.signal
}
semaphore.wait // -> true

Requirements

  • Swift 2.0 (Xcode 7)

Installation

Manually

To use this library in your project manually, just drag all swift files under Classes to the project tree.

TODO?

  • [x] Queue
  • [x] Async/Sync
  • [x] Apply
  • [x] After
  • [x] Semaphore
  • [ ] Barrier
  • [ ] Group
  • [ ] Source
  • [ ] Block class

Author

Francis Chong, [email protected]

License

Dispatch is available under the MIT license. See the LICENSE file for more info.