Skip to content

Instantly share code, notes, and snippets.

@yakushevichsv
Created June 21, 2021 18:55
Show Gist options
  • Save yakushevichsv/c73c7f04bdcc3b46395f2c7ed80dcae4 to your computer and use it in GitHub Desktop.
Save yakushevichsv/c73c7f04bdcc3b46395f2c7ed80dcae4 to your computer and use it in GitHub Desktop.
MainThreadRunnerType.runOnMain
import UIKit
// MARK: - MainThreadRunnerType
protocol MainThreadRunnerType {
func runOnMain(_ block: @escaping () -> Void) // if not on main thread then run block asynchronously
func runOnMainWeakly<Object: AnyObject, T>(_ obj: Object,
parameter: T,
method: @escaping ((Object) -> (T) -> Void))
func runOnMainWeakly<Object: AnyObject>(_ obj: Object,
method: @escaping ((Object) -> () -> Void))
}
extension MainThreadRunnerType {
func runOnMain(_ block: @escaping () -> Void) {
guard Thread.isMainThread else {
DispatchQueue.main.async(execute: block)
return
}
block()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment