Created
January 17, 2017 16:06
-
-
Save westerlund/81d341e7c7465e4c32062013af157694 to your computer and use it in GitHub Desktop.
A boiler plate operation to be used as a subclass
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
// 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
Now you can do :
willSet { willChangeValue(for: \.isExecuting) }