swift – 试图理解异步操作子类
我试图开始在副项目中使用Operations,而不是在我的网络代码中散布基于闭包的回调以帮助消除嵌套调用.所以我正在阅读有关该主题的一些内容,我遇到了
this实现:
open class AsynchronousOperation: Operation { // MARK: - Properties private let stateQueue = DispatchQueue(label: "asynchronous.operation.state",attributes: .concurrent) private var rawState = OperationState.ready private dynamic var state: OperationState { get { return stateQueue.sync(execute: { rawState }) } set { willChangeValue(forKey: "state") stateQueue.sync(flags: .barrier,execute: { rawState = newValue }) didChangeValue(forKey: "state") } } public final override var isReady: Bool { return state == .ready && super.isReady } public final override var isExecuting: Bool { return state == .executing } public final override var isFinished: Bool { return state == .finished } public final override var isAsynchronous: Bool { return true } // MARK: - NSObject private dynamic class func keyPathsForValuesAffectingIsReady() -> Set<String> { return ["state"] } private dynamic class func keyPathsForValuesAffectingIsExecuting() -> Set<String> { return ["state"] } private dynamic class func keyPathsForValuesAffectingIsFinished() -> Set<String> { return ["state"] } // MARK: - Foundation.Operation public final override func start() { super.start() if isCancelled { finish() return } state = .executing execute() } // MARK: - Public /// Subclasses must implement this to perform their work and they must not call `super`. The default implementation of this function throws an exception. open func execute() { fatalError("Subclasses must implement `execute`.") } /// Call this function after any work is done or after a call to `cancel()` to move the operation into a completed state. public final func finish() { state = .finished } } @objc private enum OperationState: Int { case ready case executing case finished } 这个Operation子类有一些实现细节,我想在理解上有所帮助. > stateQueue属性的目的是什么?我看到它被状态计算属性的get和set使用,但我找不到任何解释sync的文档:flags:execute和sync:执行他们使用的方法.
你说:
此代码“同步”对属性的访问以使其线程安全.关于为什么需要这样做,请参阅the
关于这个并发队列用于同步的确切用法,这被称为“读写器”模式.读写器模式的这个基本概念是读取可以相互发生并发(因此同步,没有障碍),但写入必须永远不会同时执行该属性的任何其他访问(因此与屏障异步) .这一点在WWDC 2012视频Asynchronous Design Patterns with Blocks,GCD,and XPC中有所描述.注意,虽然该视频概述了基本概念,但它使用了较旧的dispatch_sync和dispatch_barrier_async语法,而不是使用了同步和同步(flags:.barrier)语法的Swift 3及更高版本语法这里. 你还问:
这些只是确保状态属性更改触发属性 您找到的keyPathsForValuesAffectingValue方法是相关的.您可以使用该方法注册从属密钥,也可以使用原始代码段中显示的各个方法. 顺便说一句,这是您提供的AsynchronousOperation类的修订版,即: >你不能调用super.start().正如
>在Swift 4中添加@objc. 从而: public class AsynchronousOperation: Operation { /// State for this operation. @objc private enum OperationState: Int { case ready case executing case finished } /// Concurrent queue for synchronizing access to `state`. private let stateQueue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".rw.state",attributes: .concurrent) /// Private backing stored property for `state`. private var _state: OperationState = .ready /// The state of the operation @objc private dynamic var state: OperationState { get { return stateQueue.sync { _state } } set { stateQueue.sync(flags: .barrier) { _state = newValue } } } // MARK: - Various `Operation` properties open override var isReady: Bool { return state == .ready && super.isReady } public final override var isExecuting: Bool { return state == .executing } public final override var isFinished: Bool { return state == .finished } // KVN for dependent properties open override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> { if ["isReady","isFinished","isExecuting"].contains(key) { return [#keyPath(state)] } return super.keyPathsForValuesAffectingValue(forKey: key) } // Start public final override func start() { if isCancelled { finish() return } state = .executing main() } /// Subclasses must implement this to perform their work and they must not call `super`. The default implementation of this function throws an exception. open override func main() { fatalError("Subclasses must implement `main`.") } /// Call this function to finish an operation that is currently executing public final func finish() { if isExecuting { state = .finished } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |