From 85443018215e521c180c5f951336cd7f615a1907 Mon Sep 17 00:00:00 2001 From: Gary Tokman Date: Sat, 8 May 2021 11:06:09 -0400 Subject: [PATCH] feat: add safe async --- Sources/ExtensionKit/Foundation/DispactQueue.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Sources/ExtensionKit/Foundation/DispactQueue.swift diff --git a/Sources/ExtensionKit/Foundation/DispactQueue.swift b/Sources/ExtensionKit/Foundation/DispactQueue.swift new file mode 100644 index 0000000..8a26116 --- /dev/null +++ b/Sources/ExtensionKit/Foundation/DispactQueue.swift @@ -0,0 +1,14 @@ +import Foundation + +extension DispatchQueue { + + /// Run closure if thread is main else switch to main and run the closure + /// - Parameter work: Closure to run + static func mainSafeAsync(execute work: @escaping () -> Void) { + if Thread.isMainThread { + work() + } else { + main.async(execute: work) + } + } +}