【问题标题】:Using Dispatch queue in swift 3 perfect 2.0在 swift 3 perfect 2.0 中使用调度队列
【发布时间】:2017-01-06 02:47:20
【问题描述】:

我正在使用 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?它们似乎不起作用。

【问题讨论】:

    标签: ios swift server dispatch perfect


    【解决方案1】:

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

    解决方案:

    let backgroundQueue = DispatchQueue(label: "com.app.queue", qos: .background, target: nil)
    
    let when = DispatchTime.now() + 10
    backgroundQueue.asyncAfter(deadline: when){
        //call function
    }
    

    【讨论】:

    • 看起来我们不应该使用主队列。感谢您的提示
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多