Skip to content

Instantly share code, notes, and snippets.

@westerlund
Created January 17, 2017 16:06
Show Gist options
  • Save westerlund/81d341e7c7465e4c32062013af157694 to your computer and use it in GitHub Desktop.
Save westerlund/81d341e7c7465e4c32062013af157694 to your computer and use it in GitHub Desktop.
A boiler plate operation to be used as a subclass
// A boiler plate operation to be used as a subclass
//
// Created by Simon Westerlund on 2016-11-07.
// Copyright © 2016 Simon Westerlund. All rights reserved.
//
import Foundation
final class BaseOperation: Operation {
private var _isFinished = false {
willSet { willChangeValue(forKey: "isFinished") }
didSet { didChangeValue(forKey: "isFinished") }
}
private var _isExecuting = false {
willSet { willChangeValue(forKey: "isExecuting") }
didSet { didChangeValue(forKey: "isExecuting") }
}
private var _isCancelled = false {
willSet { willChangeValue(forKey: "isCancelled") }
didSet { didChangeValue(forKey: "isCancelled") }
}
init() {
}
private func performOperation() {
// do stuff
done()
}
private func done() {
_isExecuting = false
_isFinished = true
}
override func start() {
_isExecuting = true
performOperation()
}
override func cancel() {
_isCancelled = true
done()
}
override var isExecuting: Bool {
return _isExecuting
}
override var isFinished: Bool {
return _isFinished
}
override var isCancelled: Bool {
return _isCancelled
}
}
@lastMove
Copy link

lastMove commented Nov 2, 2017

Now you can do :
willSet { willChangeValue(for: \.isExecuting) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment