Skip to content

Instantly share code, notes, and snippets.

@dgyesbreghs
Created January 29, 2019 18:59
Show Gist options
  • Save dgyesbreghs/7ca93d132646483fcec6f1d9456121ff to your computer and use it in GitHub Desktop.
Save dgyesbreghs/7ca93d132646483fcec6f1d9456121ff to your computer and use it in GitHub Desktop.
Thread Safety with DispatchQueue
//
// DispatchQueu+ThreadSafety.swift
// BoleroPhone
//
// Created by Dylan Gyesbreghs on 29/01/2019.
// Copyright © 2019 iCapps. All rights reserved.
//
import Foundation
extension DispatchQueue {
class func sync(_ completion: () -> Void) {
if Thread.isMainThread {
completion()
} else {
main.sync(execute: completion)
}
}
class func async(_ completion: @escaping () -> Void) {
main.async(execute: completion)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment