http - 如何在 Swift 3 中同时发出 https 请求

标签 http concurrency swift3 nsoperation nsoperationqueue

我在执行 https 请求时遇到问题,如果请求没有任何错误,我永远不会收到消息,这是一个命令行工具应用程序,我有一个允许 http 请求的 plist,我总是看到完成 block 。

typealias escHandler = ( URLResponse?, Data? ) -> Void

func getRequest(url : URL, _ handler : @escaping escHandler){    
let session = URLSession.shared
var request = URLRequest(url:url)
request.cachePolicy = .reloadIgnoringLocalCacheData
request.httpMethod = "GET"
let task = session.dataTask(with: url ){ (data,response,error) in
        handler(response,data)
}

task.resume()
}


func startOp(action : @escaping () -> Void) -> BlockOperation{

let exOp = BlockOperation(block: action)    
exOp.completionBlock = {

print("Finished")

}
return exOp
}

     for sUrl in textFile.components(separatedBy: "\n"){
     let url = URL(string: sUrl)!

        let queu = startOp {
            getRequest(url: url){  response, data  in

                print("REACHED")



            }

        }
      operationQueue.addOperation(queu)
      operationQueue.waitUntilAllOperationsAreFinished()

最佳答案

一个问题是您的操作只是启动请求,但由于请求是异步执行的,因此操作会立即完成,而不是实际等待请求完成。在异步请求完成之前,您不想完成操作。

如果您想对操作队列执行此操作,诀窍是您必须子类化 Operation并为 isExecuting 执行必要的 KVO和 isFinished .然后你改变isExecuting当你开始请求和isFinished当你完成请求时,两者都有相关的 KVO。所有这些都在 Concurrency Programming Guide: Defining a Custom Operation Object 中进行了概述。 , 特别是在 Configuring Operations for Concurrent Execution部分。 (请注意,本指南有点过时(它指的是 isConcurrent 属性,已被替换为 isAsynchronous ;它专注于 Objective-C;等等),但它向您介绍了问题。

无论如何,这是一个抽象类,我用它来封装所有这些愚蠢的异步操作:

/// Asynchronous Operation base class
///
/// This class performs all of the necessary KVN of `isFinished` and
/// `isExecuting` for a concurrent `NSOperation` subclass. So, to developer
/// a concurrent NSOperation subclass, you instead subclass this class which:
///
/// - must override `main()` with the tasks that initiate the asynchronous task;
///
/// - must call `completeOperation()` function when the asynchronous task is done;
///
/// - optionally, periodically check `self.cancelled` status, performing any clean-up
///   necessary and then ensuring that `completeOperation()` is called; or
///   override `cancel` method, calling `super.cancel()` and then cleaning-up
///   and ensuring `completeOperation()` is called.

public class AsynchronousOperation : Operation {

    override public var isAsynchronous: Bool { return true }

    private let lock = NSLock()

    private var _executing: Bool = false
    override private(set) public var isExecuting: Bool {
        get {
            return lock.synchronize { _executing }
        }
        set {
            willChangeValue(forKey: "isExecuting")
            lock.synchronize { _executing = newValue }
            didChangeValue(forKey: "isExecuting")
        }
    }

    private var _finished: Bool = false
    override private(set) public var isFinished: Bool {
        get {
            return lock.synchronize { _finished }
        }
        set {
            willChangeValue(forKey: "isFinished")
            lock.synchronize { _finished = newValue }
            didChangeValue(forKey: "isFinished")
        }
    }

    /// Complete the operation
    ///
    /// This will result in the appropriate KVN of isFinished and isExecuting

    public func completeOperation() {
        if isExecuting {
            isExecuting = false
            isFinished = true
        }
    }

    override public func start() {
        if isCancelled {
            isFinished = true
            return
        }

        isExecuting = true

        main()
    }
}

我将这个 Apple 扩展程序用于 NSLocking以确保我同步上面的状态更改(他们是在 withCriticalSection 上称为 NSLock 的扩展,但这是一个稍微更通用的再现,处理任何符合 NSLocking 并处理抛出错误的闭包) :

extension NSLocking {

    /// Perform closure within lock.
    ///
    /// An extension to `NSLocking` to simplify executing critical code.
    ///
    /// - parameter block: The closure to be performed.

    func synchronize<T>(block: () throws -> T) rethrows -> T {
        lock()
        defer { unlock() }
        return try block()
    }
}

然后,我可以创建一个 NetworkOperation它使用那个:

class NetworkOperation: AsynchronousOperation {
    var task: URLSessionTask!

    init(session: URLSession, url: URL, requestCompletionHandler: @escaping (Data?, URLResponse?, Error?) -> ()) {
        super.init()

        task = session.dataTask(with: url) { data, response, error in
            requestCompletionHandler(data, response, error)
            self.completeOperation()
        }
    }

    override func main() {
        task.resume()
    }

    override func cancel() {
        task.cancel()
        super.cancel()
    }
}

无论如何,完成后,我现在可以为网络请求创建操作,例如:

let queue = OperationQueue()
queue.name = "com.domain.app.network"

let url = URL(string: "http://...")!
let operation = NetworkOperation(session: .shared, url: url) { data, response, error in
    guard let data = data, error == nil else {
        print("\(error)")
        return
    }

    let string = String(data: data, encoding: .utf8)
    print("\(string)")
    // do something with `data` here
}

let operation2 = BlockOperation {
    print("done")
}

operation2.addDependency(operation)

queue.addOperations([operation, operation2], waitUntilFinished: false) // if you're using command line app, you'd might use `true` for `waitUntilFinished`, but with standard Cocoa apps, you generally would not

请注意,在上面的示例中,我添加了第二个操作来打印一些内容,使其依赖于第一个操作,以说明第一个操作在网络请求完成之前不会完成。

显然,您通常不会使用 waitUntilAllOperationsAreFinished您的原始示例,也不是 waitUntilFinished addOperations 的选项在我的例子中。但是因为您正在处理一个您不想在这些请求完成之前退出的命令行应用程序,所以这种模式是可以接受的。 (我提到这一点只是为了 future 的读者,他们对 waitUntilFinished 的随心所欲的使用感到惊讶,这通常是不可取的。)

关于http - 如何在 Swift 3 中同时发出 https 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40558679/

相关文章:

concurrency - 计算/显示事件 goroutine 的数量

ios - swift 3.1 : Crash when custom error is converted to NSError to access its domain property

multithreading - 在 Swift 中设置和读取 Bool 原子操作吗?

ios - 如何从 Appdelegate 显示 UIAlertController

python - Django:如何模拟慢速服务器?

python - 错误 403 : HTTP status code is not handled or not allowed in scrapy

c++ - C++ STL (ExecutionPolicy) 算法如何确定使用多少并行线程?

http - 在线 HTTP 客户端

java - Last-Modified 与 ETag http

concurrency - 组合 Task.async_stream 与 Continuation 传递