Skip to content

Instantly share code, notes, and snippets.

@AndrewBarba
Created August 30, 2015 23:28
Show Gist options
  • Save AndrewBarba/97d3a0803bf6d610e358 to your computer and use it in GitHub Desktop.
Save AndrewBarba/97d3a0803bf6d610e358 to your computer and use it in GitHub Desktop.
//
// Dispatch.swift
//
// Created by Andrew Barba on 8/25/15.
//
import Foundation
public struct Dispatch {
// MARK: - Async
/**
Asynchronously dispatch a block on a background thread
*/
public static func async(block: ()->()) {
let queue = dispatch_queue_create("com.tablelist.Tablelist.queue.async", nil)
async(queue, block: block)
}
public static func async(queue: dispatch_queue_t, block: ()->()) {
dispatch_async(queue, block)
}
/**
Asynchronously dispatch a block on a background thread after a specified number of seconds
*/
public static func async(queue: dispatch_queue_t, time: Double, block: ()->()) {
let after = dispatch_time(DISPATCH_TIME_NOW, Int64(time * Double(NSEC_PER_SEC)))
dispatch_after(after, queue, block)
}
public static func async(time: Double, block: ()->()) {
let queue = dispatch_queue_create("com.tablelist.Tablelist.queue.async.after", nil)
async(queue, time: time, block: block)
}
// MARK: - Main
/**
Asynchronously dispatch a block on the main thread
*/
public static func main(block: ()->()) {
async(dispatch_get_main_queue(), block: block)
}
/**
Asynchronously dispatch a block on the main thread after a specified number of seconds
*/
public static func main(time: Double, block: ()->()) {
async(dispatch_get_main_queue(), time: time, block: block)
}
// MARK: - Synchronized
/**
Swift function for ObjC @synchronized
*/
public static func sync(queue: dispatch_queue_t, block: () -> ()) {
dispatch_sync(queue, block)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment