Skip to content

Instantly share code, notes, and snippets.

@kakilangit
Last active January 15, 2016 09:51
Show Gist options
  • Select an option

  • Save kakilangit/29058d187e58a0960edc to your computer and use it in GitHub Desktop.

Select an option

Save kakilangit/29058d187e58a0960edc to your computer and use it in GitHub Desktop.
Background Thread Reusable function
func backgroundThread(delay: Double = 0.0, background: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue), 0)) {
if(background != nil){ background!(); }
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(popTime, dispatch_get_main_queue()) {
if(completion != nil){ completion!(); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment