Last active
July 18, 2017 03:40
-
-
Save powhu/725687d2e6c3dff61358a1f4ebc91b12 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 UIKit | |
import PlaygroundSupport | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
class MyOperation: Operation { | |
enum State { | |
case ready, executing, finished | |
var keyPath: String { | |
switch self { | |
case .ready: | |
return "isReady" | |
case .executing: | |
return "isExecuting" | |
case .finished: | |
return "isFinished" | |
} | |
} | |
} | |
var state = State.ready { | |
willSet { | |
willChangeValue(forKey: newValue.keyPath) | |
willChangeValue(forKey: state.keyPath) | |
} | |
didSet { | |
didChangeValue(forKey: oldValue.keyPath) | |
didChangeValue(forKey: state.keyPath) | |
} | |
} | |
// MARK: - NSOperation | |
override var isReady: Bool { | |
return super.isReady && state == .ready | |
} | |
override var isExecuting: Bool { | |
return state == .executing | |
} | |
override var isFinished: Bool { | |
return state == .finished | |
} | |
override var isAsynchronous: Bool { | |
return true | |
} | |
var url: URL | |
init(url: URL) { | |
self.url = url | |
} | |
override func start() { | |
guard !isCancelled else { | |
state = .finished | |
return | |
} | |
state = .executing | |
main() | |
} | |
override func main() { | |
URLSession.shared.dataTask(with: url, completionHandler: {(data, res, error) in | |
guard !self.isCancelled else { | |
self.state = .finished | |
return | |
} | |
if let res = res as? HTTPURLResponse { | |
print("\(self.url.absoluteString) : \(res.statusCode)") | |
} | |
self.state = .finished | |
} ).resume() | |
} | |
} | |
class MyObject: NSObject { | |
override init() { | |
super.init() | |
let o1 = MyOperation(url: URL(string:"https://www.google.com")!) | |
let o2 = MyOperation(url: URL(string:"https://www.gamer.com.tw")!) | |
let o3 = MyOperation(url: URL(string:"https://twitter.com/paste_app")!) | |
let o4 = MyOperation(url: URL(string:"https://tw.yahoo.com")!) | |
let o5 = MyOperation(url: URL(string:"https://www.yahoo.co.jp")!) | |
let queue = OperationQueue() | |
queue.maxConcurrentOperationCount = 3 | |
let finishedOpration = BlockOperation { | |
print("Block Complete") | |
} | |
for o in [o1,o2,o3,o4,o5] { | |
queue.addOperation(o) | |
} | |
queue.addOperation(finishedOpration) | |
queue.addObserver(self, forKeyPath: "operationCount", options: [.new], context: nil) | |
} | |
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | |
if let q = object as? OperationQueue { | |
print("\(q.operationCount)") | |
} | |
} | |
} | |
let a = MyObject() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment