Delay calls using DispatchQueue.asyncAfter [duplicate]
Delay calls using DispatchQueue.asyncAfter [duplicate]
This question already has an answer here:
I've been trying to understand how the asyncAfter method works in DispatchQueue so I could implement it in my code to delay network calls. I've tried the suggestions from this thread to no avail:
How do I write dispatch_after GCD in Swift 3 and 4?
asyncAfter
DispatchQueue
In my test code only the first print statement gets printed. How come the second print doesn't print?
func queueWithDelay()
let delayQueue = DispatchQueue(label: "testqueue")
print("First: (Date())")
delayQueue.asyncAfter(deadline: .now() + .seconds(1))
print("Second: (Date())")
queueWithDelay()
Thank you in advance.
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Yes, it's playground. The duplicate link solved the problem. Thanks!
– bvh
Sep 9 '18 at 22:08
1 Answer
1
Are you testing it in a Playground?
If yes try import PlaygroundSupport
import PlaygroundSupport
and set
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.needsIndefiniteExecution = true
You have to say to the playgroundPage to not stop when the compiler reaches the end of the file but to wait
This was a duplicate. No need to post this answer.
– rmaddy
Sep 9 '18 at 21:57
He didn't say anything about if it's running the code above in a playground. So I can't tell you if it's a duplicate
– ndPPPhz
Sep 9 '18 at 22:02
OK, either your answer is irrelevant (if they are not using a playground) or it's a duplicate (if they are using a playground). So either way there is no need for the answer. If the OP states they are not using a playground then the question can be reopened.
– rmaddy
Sep 9 '18 at 22:05
Are you running this in a playground?
– rmaddy
Sep 9 '18 at 21:55