Skip to content

Instantly share code, notes, and snippets.

@dimohamdy
Created January 6, 2019 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimohamdy/46de5c03b2ceccb1f98ed43b25a78429 to your computer and use it in GitHub Desktop.
Save dimohamdy/46de5c03b2ceccb1f98ed43b25a78429 to your computer and use it in GitHub Desktop.
using AsyncOperation
import AsyncOperation
let op1 = AsyncBlockOperation() { operation in
operation.state = .executing
let urlString = URL(string: "http://jsonplaceholder.typicode.com/users/1")
if let url = urlString {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error)
} else {
if let usableData = data {
print("op1") //JSONSerialization
operation.state = .finished
}
}
}
task.resume()
}
}
let op2 = AsyncBlockOperation() { operation in
operation.state = .executing
let urlString = URL(string: "http://jsonplaceholder.typicode.com/users/2")
if let url = urlString {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error)
} else {
if let usableData = data {
print("op2") //JSONSerialization
operation.state = .finished
}
}
}
task.resume()
}
}
let op3 = AsyncBlockOperation() { operation in
operation.state = .executing
let urlString = URL(string: "http://jsonplaceholder.typicode.com/users/3")
if let url = urlString {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error)
} else {
if let usableData = data {
print("op3") //JSONSerialization
operation.state = .finished
}
}
}
task.resume()
}
}
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
queue.addOperations([op1,op2,op3], waitUntilFinished: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment