In iOS env, is it possible to make current thread sleep for seconds, then execute my code? NSTimer, GDC or any technique is okay for me.
-
1What have you tried? Please share the code you have tried already. Also have you tried something as simple as `sleep(2);` or `[NSThread sleepForTimeInterval:2000]`? – Popeye Mar 06 '15 at 09:43
-
What do you need to achieve? – Lorenzo B Mar 06 '15 at 09:45
-
@flexaddicted I don't see it as a direct duplicate of *that*. Judging from the tags used by **Opart Code**, he does not seem to be referring to Java in any way. He may not even know any Java at all and wouldn't consider searching for this information in this way. But of course duplicate it is, there is a bunch of questions like this on SO... – Michal Mar 06 '15 at 09:51
-
1@Michal I agree with you. Anyway a simple search will give a lot of results on the subject. – Lorenzo B Mar 06 '15 at 10:16
-
1Thanks for all reply. I learn lot more from **Popeye** posted thread – Opart Code Mar 10 '15 at 04:20
5 Answers
It would be better if you shared what you have done but it here you go.
There are a few options you can go with:
Option 1
// Standard Unix calls
sleep();
usleep();
Some documentation regarding the sleep
function can be found here. You'll find that they are actually C
functions but since Objective-C
is a strict superset of C we can still use the sleep
and usleep
functions.
Option 2
[NSThread sleepForTimeInterval:2.000];//2 seconds
The Apple documentation for this method states:
Sleeps the thread for a given time interval.
Discussion
No run loop processing occurs while the thread is blocked.
Option 3
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
1 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^{
// Do whatever you want here.
});
The Grand Central Dispatch
route is a pretty good way of doing things as well. Here is the Apple Documentation for Grand Central Dispatch which is quite a good read.
There is also this question that might be pretty useful How to Wait in Objective-C

- 17,282
- 18
- 107
- 195

- 11,839
- 9
- 58
- 91
-
1
-
-
-
@NSTJ I'm confused at what you are asking. Are you talking about the OP sharing code in their post? If so it is recommended https://stackoverflow.com/help/how-to-ask to improve the question and help find a solution that suits them best and many other reasons why it would be better to share code including shows that they have attempted to resolve their question themselves. – Popeye May 02 '17 at 13:04
-
Indeed - it seems like the OP may not have actually done anything yet (which is fine) and I can't see the issue with asking a well structured question like he did without posting any code. Also from the link you gave: > Not all questions benefit from including code – NSTJ May 02 '17 at 13:52
-
`[NSThread sleepForTimeInterval:2000];` this will sleep for 2000 seconds. I'm editing because most will assume that's 2000 milliseconds and be confused with behavior (as already seen via @Itai Spector in comments above) – Albert Renshaw Feb 08 '18 at 22:48
it cannot be easier.
sleep(seconds);

- 7,426
- 6
- 39
- 59
-
Thank you for comment, It looks like a system level API to stop current thread. Anyway, it works for my situation. – Opart Code Mar 10 '15 at 04:22
-
1Why not? If the answer is really simple, I don't know...what do you expect? – Earl Grey Mar 11 '15 at 12:23
Use the class method + (void)sleepForTimeInterval:(NSTimeInterval)ti
The variable NSTimeInterval is of type double and represents the number of seconds to sleep
// Block for .5 seconds
[NSThread sleepForTimeInterval:.5];

- 4,826
- 4
- 27
- 54
Either
[self performSelector:@selector(YourFunctionName) withObject:(can be Self or Object from other Classes) afterDelay:(Time Of Delay)];`
or
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
//your method
});
it depends how you are creating (spawning) your threads. For example if you are creating your thread with NSThread
class, you can use the two class methods :
sleepUntilDate:
sleepForTimeInterval:
But generally it's a bad idea to handle the threading management yourself, because multithreading programming is very hard. You can use GCD or operations queues for example to handle the multithreading in your application.

- 4,501
- 6
- 49
- 76