Using Dispatch queue in swift 3 perfect 2.0

273 views Asked by At

I'm using swift perfect 2.0 and i need to call a function after 10 seconds. I can make it work on a normal iOS application with this code:

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

But i can't do this in swift perfect, and i don't know how to work arround. This is the structure of my request:

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()
    }
{

It never calls sendNotifications(), even if i place it before response.completed(), and i'm probably thinking wrong. So my question is, is there any other way to use Dispatchqueues in perfect 2.0? They don't seem to work.

2

There are 2 answers

1
Phuzer On BEST ANSWER

Ok, now i understand. I can't block main queue in swift perfect.

solution:

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

let when = DispatchTime.now() + 10
backgroundQueue.asyncAfter(deadline: when){
    //call function
}
0
Jake On

It looks like perfect 2.0 has it's own thread management setup. Checkout this link:

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