Skip to content

Instantly share code, notes, and snippets.

@westerlund
Last active January 17, 2017 16:28
Show Gist options
  • Save westerlund/d0a49787726205b94744d6b0795b61dd to your computer and use it in GitHub Desktop.
Save westerlund/d0a49787726205b94744d6b0795b61dd to your computer and use it in GitHub Desktop.
A boiler plate operation to be used as a subclass, rename and implement your own operation stuff
// 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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment