高级 NSOperations,GroupOperation 的回调

Advanced NSOperations, callback for GroupOperation

我目前正在尝试使用 WWDC 期间介绍的有关高级 NSOperations 的概念。我想将发出请求的过程与不同的 Operation 子 class 分开(并通过扩展任何类型的链式操作)。

我使用上下文 class 在流程中涉及的所有操作之间共享数据(参见:

class RequestContext {

    var httpResponse:NSHTTPURLResponse?
    var data:NSData?
    var JSON:AnyObject?
}

每个子class在初始化期间获得对上下文的引用。

下载示例:

class DownloadOperation : GroupOperation {

    let requestContext:RequestContext


    init(context:RequestContext, apiCall:API){
        requestContext = context
        super.init(operations: [])
        name = "Generic Download"

        guard let URL = apiCall.URLRequest else {
            finish()
            return
        }

        let dataTask  = NSURLSession.sharedSession().dataTaskWithURL(URL) {
            data, response, error in

            self.downloadFinished(data, response: response as? NSHTTPURLResponse, error: error)
        }

        let downloadTask = URLSessionTaskOperation(task: dataTask)

        let reachabilityCondition = ReachabilityCondition(host: URL)

        let networkObserver = NetworkObserver()

        downloadTask.addCondition(reachabilityCondition)

        downloadTask.addObserver(networkObserver)

        addOperation(downloadTask)
    }


    private func downloadFinished(data:NSData?, response:NSHTTPURLResponse?, error:NSError?){
        if let data = data {
            requestContext.data = data
            requestContext.httpResponse = response
        } else {
            finishWithError(error)
        }
    }
}

示例JSON解析:

class JSONUnmarshallOperation: Operation {

        let context:RequestContext

        init(context c:RequestContext){
            self.context = c

            super.init()

            name = "JSON Parsing"
        }

        final override func execute() {
            guard let data = context.data else { finish(); return }

            defer {
                context.data = nil
            }

            do {
                context.JSON = try NSJSONSerialization.JSONObjectWithData(data, options: [])
                finish()
            } catch let error as NSError {
                finishWithError(error)
            }

        }
    }

我希望能够创建一个依赖于这两个操作的 GroupOperation 并定义一个完成回调。我想要这样的东西:

 class SomeOperation: GroupOperation {

    let downloadOperation:DownloadOperation
    let unmarshalOperation:JSONUnmarshallOperation

    init (completionHandler:<* Some Type *>) {

        let blockOperation = NSBlockOperation {

            //Use the callback here and access self
        }


        super.init(operations:[downloadOperation, unmarshalOperation, blockOperation])
    }
}

这里的问题是,我会在调用 super.init

之前访问 self

是否有一种简单而优雅的方法来添加通用完成处理程序添加 GroupOperation subclass 的结尾?

简单的答案是在您调用 super.init:

之前不要构建您的 blockOperation
init (completionHandler:<* Some Type *>) {
    let downloadOperation = ...
    let unmarshallOperation = ...
    unmarshallOperation.addDependency(downloadOperation)

    super.init(operations:[downloadOperation, unmarshallOperation, blockOperation])

    let blockOperation = NSBlockOperation {
        //Use the callback here and access self
    }
    blockOperation.addDependency(unmarshallOperation)
    addOperation(blockOperation)
}