1

我正在使用 swift perfect 2.0,我需要在 10 秒后调用一个函数。我可以使用以下代码使其在普通的 iOS 应用程序上运行:

let when = DispatchTime.now() + 10
DispatchQueue.main.asyncAfter(deadline: when){
    //call function
}

但是我不能快速完美地做到这一点,而且我不知道如何工作。这是我的请求的结构:

public func InsertPost(_ request: HTTPRequest, response: HTTPResponse)

    //logic

    response.status = .custom(code: 200, message: "Success!")
    response.completed()

    //here i want to send a notification to some users, but only after 10 seconds. 
    //So i try to call function sendNotifications() like this:

    let when = DispatchTime.now() + 10
    DispatchQueue.main.asyncAfter(deadline: when){
        sendNotifications()
    }
{

它从不调用 sendNotifications(),即使我把它放在 response.completed() 之前,我可能想错了。所以我的问题是,有没有其他方法可以在完美 2.0 中使用 Dispatchqueues?它们似乎不起作用。

4

2 回答 2

2

好的,现在我明白了。我无法快速完美地阻止主队列。

解决方案:

let backgroundQueue = DispatchQueue(label: "com.app.queue", qos: .background, target: nil)

let when = DispatchTime.now() + 10
backgroundQueue.asyncAfter(deadline: when){
    //call function
}
于 2017-01-06T15:43:15.503 回答
1

看起来完美的 2.0 有它自己的线程管理设置。查看此链接:

http://perfect.org/docs/thread.html

于 2017-01-06T03:27:01.300 回答