I'm learning OperationQueue. What I want to do is to prevent network calls if there's an ongoing call. The problem is that when I tap on the button, a new operation is being added to the queue.
class ViewController: UIViewController {
    private var queue = OperationQueue()
    func networkCall(completion: (()->Void)?) {
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
            completion?()
        }
    }
    lazy var button: UIButton = {
        let b = UIButton(type: .custom)
        b.backgroundColor = .black
        b.setTitleColor(.white, for: .normal)
        b.setTitle("CLICK!~", for: .normal)
        b.addTarget(self, action: #selector(self.boom), for: .touchUpInside)
        return b
    }()
    @objc func boom() {
        print("BOOM!")
        self.queue.maxConcurrentOperationCount = 1
        let block = BlockOperation()
        block.addExecutionBlock {
            self.networkCall {
                print("DONE NETWORK CALL!")
            }
        }
        self.queue.addOperation(block)
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
            self.queue.cancelAllOperations()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .gray
        self.view.addSubview(self.button)
        self.button.translatesAutoresizingMaskIntoConstraints = false
        self.button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        self.button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        self.button.widthAnchor.constraint(equalToConstant: 100.0).isActive = true
        self.button.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
    }
}
Am I missing something?
 
    