Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save CanTheAlmighty/fabdef60994171859a90acfc43b9d1d0 to your computer and use it in GitHub Desktop.

Select an option

Save CanTheAlmighty/fabdef60994171859a90acfc43b9d1d0 to your computer and use it in GitHub Desktop.

Revisions

  1. CanTheAlmighty revised this gist Dec 13, 2017. No changes.
  2. CanTheAlmighty revised this gist Dec 13, 2017. 2 changed files with 11 additions and 2 deletions.
    11 changes: 10 additions & 1 deletion AsynchronousBlockOperation.swift
    Original 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 markAsFinished()
    /// 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
    2 changes: 1 addition & 1 deletion AsynchronousOperation.swift
    Original 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 set `self.state = .finished`
    /// 2. When operation is finished or cancelled call `finish()`
    open class AsynchronousOperation: Operation
    {
    public enum State : String
  3. CanTheAlmighty revised this gist Dec 13, 2017. 2 changed files with 90 additions and 39 deletions.
    31 changes: 31 additions & 0 deletions AsynchronousBlockOperation.swift
    Original 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)
    }
    }
    }
    98 changes: 59 additions & 39 deletions AsynchronousOperation.swift
    Original 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`
    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 = "Ready"
    case executing = "Executing"
    case finished = "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
    }
    }
    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
    }
    }
  4. @Sorix Sorix revised this gist Mar 14, 2017. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion AsynchronousOperation.swift
    Original 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, executing, finished
    case ready = "Ready"
    case executing = "Executing"
    case finished = "Finished"
    fileprivate var keyPath: String { return "is" + self.rawValue }
    }

  5. @Sorix Sorix created this gist Feb 7, 2017.
    40 changes: 40 additions & 0 deletions AsynchronousOperation.swift
    Original 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
    }
    }
    }