Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Subclass of NSOperation to make it asynchronous in Swift 3
//
// 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 }
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
}
}
}
@rgkobashi

This comment has been minimized.

Copy link

commented Jul 28, 2017

very useful, thanks!

@iORichy

This comment has been minimized.

Copy link

commented Aug 31, 2017

works like a charm! thanks

@sphericalwave

This comment has been minimized.

Copy link

commented Dec 29, 2017

Awesome Dood!

@nickkohrn

This comment has been minimized.

Copy link

commented Jan 5, 2018

There is an issue in the steps that you provided for using this code. I made the same mistake, which can be found in my question over on Stack Overflow. According to Apple's documentation, you should not call super at any time.

@dotWasim

This comment has been minimized.

Copy link

commented Jan 13, 2018

Cool! and thanks @nickkohrn for your valuable comment!

@Sorix

This comment has been minimized.

Copy link
Owner Author

commented Feb 9, 2018

@nickkohrn, maybe you don't need to call super if you're directly subclassing Operation. But if you subclass AsynchronousOperation you have to call super, because there are some work is done inside my implementation of that methods. For example state = .executing won't be executed and your implementation will become buggy.

@zhihuitang

This comment has been minimized.

Copy link

commented Feb 14, 2018

state is not thread-safe, setting state might cause TSAN issues.

@sisoje

This comment has been minimized.

Copy link

commented Mar 4, 2018

that if else in main() though... 4 lines of code waisted

@ramunasjurgilas

This comment has been minimized.

Copy link

commented Jan 22, 2019

How I could use AsynchronousOperation in the API call?

ApiClient looks like this:

internal class ApiClient : NSObject {

    /// Data task method to invoke back-end endpoints. Errors will be returned as ApiClientError.
    internal func send(_ request: URLRequest, completion: @escaping (Data?, Error?) -> Void)
}

@Sorix

This comment has been minimized.

Copy link
Owner Author

commented Feb 17, 2019

that if else in main() though... 4 lines of code waisted

Why? You need to flag operation as executing before performing any actions, also operation maybe cancelled even before starting.

@iTamilan

This comment has been minimized.

Copy link

commented Apr 14, 2019

Awesome thanks Sorix. I made one example project to understand also.

@DanSkeel

This comment has been minimized.

Copy link

commented May 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.