Skip to content

Instantly share code, notes, and snippets.

@sjehutch
Last active February 26, 2018 05:11
Show Gist options
  • Save sjehutch/3e7cf2c51edce598ed2de8adf1be3984 to your computer and use it in GitHub Desktop.
Save sjehutch/3e7cf2c51edce598ed2de8adf1be3984 to your computer and use it in GitHub Desktop.
GCD Async / Sync / Serial / Concurrent
func doLongAsyncTaskInSerialQueue() {
let serialQueue = DispatchQueue(label: "com.queue.Serial")
for i in 1...5 {
serialQueue.async {
if Thread.isMainThread{
print("task running in main thread")
}else{
print("task running in background thread")
}
let imgURL = URL(string: "https://upload.wikimedia.org/wikipedia/commons/0/07/Huge_ball_at_Vilnius_center.jpg")!
let _ = try! Data(contentsOf: imgURL)
print("\(i) completed downloading")
}
}
}
func doLongSyncTaskInSerialQueue() {
let serialQueue = DispatchQueue(label: "com.queue.Serial")
for i in 1...5 {
serialQueue.sync {
if Thread.isMainThread{
print("task running in main thread")
}else{
print("task running in background thread")
}
let imgURL = URL(string: "https://upload.wikimedia.org/wikipedia/commons/0/07/Huge_ball_at_Vilnius_center.jpg")!
let _ = try! Data(contentsOf: imgURL)
print("\(i) completed downloading")
}
}
}
func doLongASyncTaskInConcurrentQueue() {
let concurrentQueue = DispatchQueue(label: "com.queue.Concurrent", attributes: .concurrent)
for i in 1...5 {
concurrentQueue.async {
if Thread.isMainThread{
print("task running in main thread")
}else{
print("task running in background thread")
}
let imgURL = URL(string: "https://upload.wikimedia.org/wikipedia/commons/0/07/Huge_ball_at_Vilnius_center.jpg")!
let _ = try! Data(contentsOf: imgURL)
print("\(i) completed downloading")
}
print("\(i) executing")
}
}
func doMultipleSyncTaskWithinAsynchronousOperation() {
let concurrentQueue = DispatchQueue(label: "com.queue.Concurrent", attributes: .concurrent)
concurrentQueue.async {
let concurrentQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default)
for i in 1...5 {
concurrentQueue.sync {
let imgURL = URL(string: "https://upload.wikimedia.org/wikipedia/commons/0/07/Huge_ball_at_Vilnius_center.jpg")!
let _ = try! Data(contentsOf: imgURL)
print("\(i) completed downloading")
}
print("\(i) executed")
}
}
}
doLongAsyncTaskInSerialQueue()
doLongASyncTaskInConcurrentQueue()
DispatchQueue.global(qos: .background).async {
// Do some background work
DispatchQueue.main.async {
// Update the UI to indicate the work has been completed
}
}
DispatchQueue.global(qos: .utility).sync {
// Background Task
DispatchQueue.main.sync {
// App will crash
}
}
let customSerialQueue = DispatchQueue(label: "com.yogevsitton.MyApp.myCustomSerialQueue")
customSerialQueue.sync {
// Synchronous code
customSerialQueue.sync {
// This code will never be executed and the app is now in deadlock
}
}
DispatchQueue.global(qos: .utility).async {
// Asynchronous code running on the low priority queue
}
DispatchQueue.main.async {
// Asynchronous code running on the main queue
}
DispatchQueue.global(qos: .userInitiated).sync {
// Synchronous code running on the high prioriy queue
}
doMultipleSyncTaskWithinAsynchronousOperation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment