i want to stop currently running method for a short duration.. can i do it without using any thread ?
A:
Use NSTimer:
- (void)doThing
{
[self doFirstPart];
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(doSecondPart)
userInfo:nil
repeats:NO];
}
- (void)doFirstPart { printf("Hello, "); }
- (void)doSecondPart { printf(" world!\n"); }
Todd Yandell
2010-09-24 08:46:54
+2
A:
Split the method into a myMethodPartA
and a myMethodPartB
. Then, at the end of myMethodPartA
, use the line:
[self performSelector: @selector(myMethodPartB) withObject: yourArgument afterDelay: someNSTimeInterval]
If you need to pass a bunch of information from myMethodPartA
to myMethodPartB
, you can bundle all of that information into an NSArray which you pass as the argument to myMethodPartB
.
Jon Rodriguez
2010-09-24 08:47:07
A:
You can always use the sleep()
or usleep()
functions. There might be convenient methods available on NSThread
as well, depending what you use exactly (you often work on one thread anyway).
Eiko
2010-09-24 08:48:23
That's a bad idea on your main thread. If you do that, the UI will freeze for the duration of the sleep.
JeremyP
2010-09-24 09:38:58
In general I'd agree. But there is no context given. Well, in fact he asks specifically "without using any thread", so all the performSelector: ... are technically spoken out. And he wants to *stop* which the others methods do *not* accomplish!
Eiko
2010-09-24 19:05:08