COOperation 0.0.3

COOperation 0.0.3

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Released最后发布2017年2月

Gleb Novik 维护。



  • 作者
  • Gleb Novik 和 Egor Tolstoy

你的服务实现是你核心组件的协作

COOperation 是在帮助使用 NSOperation 组织和结构化你的服务层代码的组件。

关键特性
🏰 设计美丽且可重用的业务逻辑
🙏 一键遵循 SOLID 原则
🍏 使用 Apple 在 WWDC 2015 介绍的 复合操作 概念(高级 NSOperations
轻松编写单元和集成测试

COOperation 使用 Objective-C 编写,并支持完全的 Swift 互操作性。顺便说一下,我们正在开发一个 Swift 版本!

使用

创建你的“可链操作”

import Foundation
import CompoundOperations

/// Chainable operation that performs network request
class NetworkRequestChainableOperation: ChainableOperationBase {

    /// Network client (Core-component)
    private let networkClient: NetworkClient

    init(networkClient: NetworkClient) {
        self.networkClient = networkClient

        super.init()
    }

    // MARK: Executing

    override func inputDataClass() -> AnyClass? {
        return NSURLRequest.self
    }

    override func processInputData(inputData: AnyObject?,
                                   completionBlock: ChainableOperationBaseOutputDataBlock) {

        let inputRequest = inputData as! NSURLRequest

        networkClient.performRequest(inputRequest) { (data, error) in
            completionBlock(data, error)
        }
    }
}

创建你的“复合操作”

    func obtainDataCompoundOperation(withResultBlock resultBlock: CompoundOperationResultBlock?) -> CompoundOperation {
        let networkRequestOperation = NetworkRequestChainableOperation(networkClient: NetworkClientImplementation())
        let deserializationOperation = DeserializationChainableOperation(deserializer: JSONDeserializer)

        let chainableOperations = [
            networkRequestOperation,
            deserializationOperation
        ]

        let operation = CompoundOperation.defaultCompoundOperation()
        operation.configureWithChainableOperations(chainableOperations,
                                                   resultBlock: resultBlock)

        return operation
    }

在你的服务中使用你的“复合操作”

    let compoundOperation = obtainDataCompoundOperation { (data, error) in
        // Process the result
    }

    queue.addOperation(compoundOperation) // OR compoundOperation.start()

作者

许可证

MIT

感谢

Sergey Simanov - 令人印象深刻的标志设计。