let queue = DispatchQueue(label: "com.frost.queue")
func syncQueue() {
let queue = DispatchQueue(label: "com.frost.queue")
queue.sync {
for i in 0...5 {
print("\(i)")
}
}
for i in 100...105 {
print("\(i)")
}
}
-------------------------------
Output:
1
2
3
4
5
101
102
103
104
105
func syncQueue() {
let queue = DispatchQueue(label: "com.frost.queue")
queue.async {
for i in 0...5 {
print("\(i)")
}
}
for i in 100...105 {
print("\(i)")
}
}
-------------------------------
Output:
1
2
101
102
3
4
103
104
5
105
優先級越高, 越會先被執行.
let queue1 = DispatchQueue(label: "com.frost.queue1", qos: DispatchQoS.userInitiated)
let queue2 = DispatchQueue(label: "com.frost.queue2", qos: DispatchQoS.unspecified)
queue1.async {
for i in 0...5 {
print("\(i)")
}
}
queue2.async {
for i in 100...105 {
print("\(i)")
}
}
-------------------------------
Output:
1
2
101
3
4
5
102
103
104
105
let queue = DispatchQueue(label: "com.frost.queue1", qos: .default)
queue.async {
for i in 0...5 {
print("\(i)")
}
}
queue.async {
for i in 100...105 {
print("\(i)")
}
}
-------------------------------
Output:
1
2
3
4
5
101
102
103
104
105
let queue = DispatchQueue(label: "com.frost.queue1", qos: .default, attributes: .concurrent)
queue.async {
for i in 0...5 {
print("\(i)")
}
}
queue.async {
for i in 100...105 {
print("\(i)")
}
}
-------------------------------
Output:
1
101
2
102
3
103
4
104
5
105
// to run something after 0.1 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
// your code here
}
DispatchQueue.main.async {
// 更新UI操作
}
DispatchQueue.global().async {
}