-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSTThreadSafe.swift
More file actions
70 lines (61 loc) · 1.96 KB
/
STThreadSafe.swift
File metadata and controls
70 lines (61 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// STThreadSafe.swift
// STBaseProject
//
// Created by 寒江孤影 on 2019/03/16.
//
import Dispatch
import Foundation
public enum STThreading {
/// 在主线程异步执行;如果当前已经在主线程则立即执行
public static func runOnMain(_ work: @escaping () -> Void) {
if Thread.isMainThread {
work()
} else {
DispatchQueue.main.async(execute: work)
}
}
/// 在主线程同步执行
public static func runOnMainSynchronously(_ work: @escaping () -> Void) {
if Thread.isMainThread {
work()
} else {
DispatchQueue.main.sync(execute: work)
}
}
/// 在主线程同步执行并返回结果
public static func runOnMainSynchronously<T>(_ work: @escaping () -> T) -> T {
if Thread.isMainThread {
return work()
} else {
return DispatchQueue.main.sync(execute: work)
}
}
/// 在全局后台队列异步执行
public static func runInBackground(
qos: DispatchQoS.QoSClass = .default,
_ work: @escaping () -> Void
) {
DispatchQueue.global(qos: qos).async(execute: work)
}
/// 在指定队列异步执行
public static func runAsync(on queue: DispatchQueue, _ work: @escaping () -> Void) {
queue.async(execute: work)
}
/// 在指定队列同步执行
public static func runSync(on queue: DispatchQueue, _ work: @escaping () -> Void) {
queue.sync(execute: work)
}
/// 在指定队列延迟执行
public static func run(
after delay: TimeInterval,
on queue: DispatchQueue = .main,
_ work: @escaping () -> Void
) {
queue.asyncAfter(deadline: .now() + delay, execute: work)
}
/// 在主线程延迟执行
public static func runOnMain(after delay: TimeInterval, _ work: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: work)
}
}