Created
January 6, 2019 19:20
-
-
Save dimohamdy/46de5c03b2ceccb1f98ed43b25a78429 to your computer and use it in GitHub Desktop.
using AsyncOperation
This file contains 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
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