-
-
Save dictav/52e180d8f449912a0c8fe77d53f61b1c to your computer and use it in GitHub Desktop.
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 Foundation | |
let queue = OperationQueue() | |
let session = URLSession.shared | |
if let contents = try? String(contentsOfFile: "./url.txt") { | |
let urls = contents.trimmingCharacters(in: .newlines).components(separatedBy: "\n") | |
for url in urls { | |
queue.addOperation(URLChecker(url: url)) | |
} | |
} | |
queue.waitUntilAllOperationsAreFinished() | |
class URLChecker: Operation { | |
private let url: URL | |
private let lockQueue = DispatchQueue(label: "net.dictav", attributes: .concurrent) | |
init(url str: String) { | |
url = URL(string: str)! | |
} | |
override var isAsynchronous: Bool { | |
return true | |
} | |
private var _isExecuting: Bool = false | |
override private(set) var isExecuting: Bool { | |
get { | |
return lockQueue.sync { () -> Bool in | |
return _isExecuting | |
} | |
} | |
set { | |
willChangeValue(forKey: "isExecuting") | |
lockQueue.sync(flags: [.barrier]) { | |
_isExecuting = newValue | |
} | |
didChangeValue(forKey: "isExecuting") | |
} | |
} | |
private var _isFinished: Bool = false | |
override private(set) var isFinished: Bool { | |
get { | |
return lockQueue.sync { () -> Bool in | |
return _isFinished | |
} | |
} | |
set { | |
willChangeValue(forKey: "isFinished") | |
lockQueue.sync(flags: [.barrier]) { | |
_isFinished = newValue | |
} | |
didChangeValue(forKey: "isFinished") | |
} | |
} | |
override func start() { | |
isFinished = false | |
isExecuting = true | |
main() | |
} | |
override func main() { | |
session.dataTask(with: URLRequest(url: self.url)) { (data, response, error) in | |
defer { | |
self.isExecuting = false | |
self.isFinished = true | |
} | |
if let err = error { | |
print(self.url, err.localizedDescription) | |
return | |
} | |
if let res = response as? HTTPURLResponse { | |
print("status: \(res.statusCode)") | |
} | |
}.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment