Created
June 21, 2021 18:55
-
-
Save yakushevichsv/c73c7f04bdcc3b46395f2c7ed80dcae4 to your computer and use it in GitHub Desktop.
MainThreadRunnerType.runOnMain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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