I want to pause my app at a certain in point. In other words, I want my app to execute the code, but then at a certain point, pause for 4 seconds, and then continue on with the rest of the code. How can I do this?
I am using Swift.
delayiosswiftxcode
I want to pause my app at a certain in point. In other words, I want my app to execute the code, but then at a certain point, pause for 4 seconds, and then continue on with the rest of the code. How can I do this?
I am using Swift.
Best Answer
Using a
dispatch_after
block is in most cases better than usingsleep(time)
as the thread on which the sleep is performed is blocked from doing other work. when usingdispatch_after
the thread which is worked on does not get blocked so it can do other work in the meantime.If you are working on the main thread of your application, using
sleep(time)
is bad for the user experience of your app as the UI is unresponsive during that time.Dispatch after schedules the execution of a block of code instead of freezing the thread:
Swift ≥ 3.0
Swift < 3.0
Swift ≥ 5.5 in an async context: