-
-
Save CanTheAlmighty/fabdef60994171859a90acfc43b9d1d0 to your computer and use it in GitHub Desktop.
Revisions
-
CanTheAlmighty revised this gist
Dec 13, 2017 . No changes.There are no files selected for viewing
-
CanTheAlmighty revised this gist
Dec 13, 2017 . 2 changed files with 11 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,18 @@ // // AsynchronousBlockOperation.swift // // Created by Jose Canepa on 12/13/17. // Copyright © 2017 Jose Canepa. All rights reserved. // import Foundation /// Block based version of the AsynchronousOperation /// /// Has the same qualities as the AsynchronousOperation, but with an implementation /// closer to the one of a BlockOperation. /// /// Once the inner asynchronous operation is finished, call the finish() /// on the block reference passed. Do not retain the passed block. /// final class AsynchronousBlockOperation : AsynchronousOperation This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import Foundation /// Subclass of `Operation` that add support of asynchronous operations. /// ## How to use: /// 1. Call `super.main()` when override `main` method, call `super.start()` when override `start` method. /// 2. When operation is finished or cancelled call `finish()` open class AsynchronousOperation: Operation { public enum State : String -
CanTheAlmighty revised this gist
Dec 13, 2017 . 2 changed files with 90 additions and 39 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ /// Block based version of the AsynchronousOperation /// /// Has the same qualities as the AsynchronousOperation, but with an implementation /// closer to the one of a BlockOperation. /// /// Once the inner asynchronous operation is finished, call the markAsFinished() /// on the block reference passed. Do not retain the passed block. /// final class AsynchronousBlockOperation : AsynchronousOperation { private var block : (AsynchronousBlockOperation) -> () init(_ block : @escaping (AsynchronousBlockOperation) -> ()) { self.block = block } override func main() { // If we were executing already let wasExecuting = (state == .executing) // Call our main to ensure we are not cancelled super.main() if !wasExecuting { block(self) } } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,43 +11,63 @@ import Foundation /// ## How to use: /// 1. Call `super.main()` when override `main` method, call `super.start()` when override `start` method. /// 2. When operation is finished or cancelled set `self.state = .finished` open class AsynchronousOperation: Operation { public enum State : String { case ready = "Ready" case executing = "Executing" case finished = "Finished" fileprivate var keyPath: String { return "is" + self.rawValue } } override open var isAsynchronous : Bool { return true } override open var isExecuting : Bool { return state == .executing } override open var isFinished : Bool { return state == .finished } public private(set) var state = State.ready { willSet(n) { willChangeValue(forKey: state.keyPath) willChangeValue(forKey: n.keyPath) } didSet(o) { didChangeValue(forKey: state.keyPath) didChangeValue(forKey: o.keyPath) } } override open func start() { if self.isCancelled { state = .finished } else { state = .ready main() } } override open func main() { if self.isCancelled { state = .finished } else { state = .executing } } public func finish() { guard state != .finished else { return } state = .finished } } -
Sorix revised this gist
Mar 14, 2017 . 1 changed file with 14 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,16 @@ // // AsynchronousOperation.swift // // Created by Vasily Ulianov on 09.02.17. // Copyright © 2017 Vasily Ulianov. All rights reserved. // import Foundation /// Subclass of `Operation` that add support of asynchronous operations. /// ## How to use: /// 1. Call `super.main()` when override `main` method, call `super.start()` when override `start` method. /// 2. When operation is finished or cancelled set `self.state = .finished` class AsynchronousOperation: Operation { override var isAsynchronous: Bool { return true } override var isExecuting: Bool { return state == .executing } @@ -17,7 +28,9 @@ class AsynchronousOperation: Operation { } enum State: String { case ready = "Ready" case executing = "Executing" case finished = "Finished" fileprivate var keyPath: String { return "is" + self.rawValue } } -
Sorix created this gist
Feb 7, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ import Foundation class AsynchronousOperation: Operation { override var isAsynchronous: Bool { return true } override var isExecuting: Bool { return state == .executing } override var isFinished: Bool { return state == .finished } var state = State.ready { willSet { willChangeValue(forKey: state.keyPath) willChangeValue(forKey: newValue.keyPath) } didSet { didChangeValue(forKey: state.keyPath) didChangeValue(forKey: oldValue.keyPath) } } enum State: String { case ready, executing, finished fileprivate var keyPath: String { return "is" + self.rawValue } } override func start() { if self.isCancelled { state = .finished } else { state = .ready main() } } override func main() { if self.isCancelled { state = .finished } else { state = .executing } } }