Last active
October 9, 2020 07:02
-
-
Save andreconghau/5b782f9dda965a01eb1547614d71afe6 to your computer and use it in GitHub Desktop.
GCD in Swift 5
This file contains hidden or 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
let queue = DispatchQueue(label: "queue_action_1") | |
queue.async { | |
for i in 1...5 { | |
print("async=" + String(i)) | |
} | |
} | |
for i in 1...5 { | |
print("sync=" + String(i)) | |
} |
This file contains hidden or 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
// DEMO về bất đồng bộ với GROUP limit timer khi dùng GCD | |
let queue2 = DispatchQueue(label: "queue_action_2") | |
let queue_group = DispatchGroup() | |
queue_group.enter() | |
queue2.async { | |
for i in 0...5 { | |
print("in group =" + String(i)) | |
sleep(1) | |
} | |
queue_group.leave() | |
} | |
// Kiểm tra status sau 2s xe đã xử lý xong group phía trên hay chưa | |
let queue_status = queue_group.wait(timeout: DispatchTime.now() + 1) | |
print(queue_status) | |
for i in 1...5 { | |
print("out group =" + String(i)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment