等待请求完成,然后再执行下一个操作 [英] Waiting for request to finish before executing the next operation

查看:726
本文介绍了等待请求完成,然后再执行下一个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在执行另一段代码之前,如何等待发送请求的操作以完成上载(并获得响应).我尝试使用NSOperations做到这一点:

I'm wanting to know how to wait for an operation that is sending a request to finish uploading (and get response) before executing another bit of code. I've attempted to do this with NSOperations:

    let testOp = BlockOperation {
        var result = 0

        for i in 1...1000000000 {
            result += i
        }
    }

    let logOp = BlockOperation {
        self.debug.log(tag: "test", content: "testing")
    }

    logOp.completionBlock = {
        print("------- logOp completed")
    }

    logOp.addDependency(testOp)

    let sendOp = BlockOperation {
        self.debug.sendLog() //uploads log, using URLSession.shared.dataTask
    }

    sendOp.completionBlock = {
        print("------- sendOp completed")
    }

    sendOp.addDependency(logOp)

    let emptyOp = BlockOperation {
        self.debug.empty()
    }

    emptyOp.completionBlock = {
        print("------- emptyOp completed")
    }

    emptyOp.addDependency(sendOp)

    let queue = OperationQueue()
    queue.addOperations([testOp, logOp, sendOp, emptyOp], waitUntilFinished: false)

输出:

------- logOp completed
*** Sending debug log (coming from self.debug.sendLog())
------- sendOp completed
------- emptyOp completed
*** sendLog uploaded (coming from self.debug.sendLog())

根据我的代码,我希望输出为:

Based on my code I expected the output to be:

------- logOp completed
*** Sending debug log
*** sendLog uploaded
------- sendOp completed
------- emptyOp completed

我如何做到这一点?我可以使用NSOperations做到这一点吗?

How do I make it so? Can I do this with NSOperations?

有关debug.sendLog()函数的其他详细信息:

Additional detail about the debug.sendLog() function:

func sendLog() {
    ...
    var urlRequest = URLRequest(url: url!)
    urlRequest.httpMethod = "POST"
    ...

    let session = URLSession.shared
    let task = session.dataTask(with: urlRequest, completionHandler: {
        (data, response, error) in

        ...

        DispatchQueue.main.async {
            print("*** sendLog uploaded")

        }
    })

    task.resume()
}

推荐答案

您可以将完成代码块直接添加到sendLog方法中,并丢弃所有代码块操作内容.

You could add the completion block straight to the sendLog method and ditch all the block operation stuff.

func sendLog(withCompletion completion: (() -> Void)) {
    ...
    var urlRequest = URLRequest(url: url!)
    urlRequest.httpMethod = "POST"
    ...

    let session = URLSession.shared
    let task = session.dataTask(with: urlRequest, completionHandler: {
        (data, response, error) in

        ...
        completion?()
    })

    task.resume()
}

然后按如下所示调用您的sendLog方法:

Then call your sendLog method like this:

sendLog(withCompletion: {
   // Execute the code you want to happen on completion here.
})

这篇关于等待请求完成,然后再执行下一个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆