views:

3733

answers:

3

I was wondering if anyone knows of a class in objective-C that stops/delays the application at a specified amount of time during runtime? I've already looked at the Time Manager and NSTimer and don't see how I could possibly put a delay in there. I've never used these two classes before though, so maybe someone else has and could give me a tip? Thanks!

A: 

I think you're looking for [NSThread sleepForTimeInterval:] That should do the trick. You should also consider using a timer to invoke another function after a delay, because that solution is non-blocking. The NSThread approach will totally lock your application for the time interval if you call it from the main thread.

Ben Gotow
It will lock the UI, it will not lock any background threads that may be doing stuff, including framework threads like the http loader.
Louis Gerbarg
+2  A: 

It really depends what you mean by "stops/delays the app." If you have a single threaded app you can just use anyone of the various sleep calls (usleep, sleep, nanosleep, etc). That is probably not what you want though, because it will just result in a hung UI for the user, and won't stop any background threads.

To actually give a decent answer it is probably necessary to know what you are actually trying to do. Are you waiting for something, polling a resource, etc?

Louis Gerbarg
well actually, my application is running on a runloop and it is getting stuck at a certain point (which I don't know where at the moment). I placed a label in my GUI just so I could put a time delay in my app's runloop right after a label.text = "Application Running Until Here". I figured this would be the most efficient way to see exactly where my app is getting hung.
Josh Bradley
You mean a UILabel? I do not think that will do what you want. All of the screen drawing is deferred until the runloop executes, so "label.text = @"Application Running here"; sleep(3);" will pause for 3 seconds after you set the labels text value, but before the screen updates. If your runloop jams for some other reason after that the label will never update on screen, even though you have set the text value. If you want actual time stamps use one of the debugging apps, or NSLog to console.
Louis Gerbarg
Well, I agree with you on the NSLog debugging. How exactly would you do that if your running an application though (I know, just put NSLog@"someText") I've tried to implement it before in my application and when I run it on the simulator, nothing ever shows up in the terminal. Also, the problem I will probably run into is that I implemented a runloop similar to the one in GKTank which uses the P2P bluetooth connectivity capabilities of the iPod Touch/iPhone and the simulator doesn't currently support that. So what way would you suggest me going to attack this problem?
Josh Bradley
It doesn't show in Terminal, how it would know what ttyy to send it to? NSLog shows up in the tty that launches it (in Xcode that will be the Debugger window or the Console window depending on what you are doing. If it is not launched from a shell NSLog output will show up in the system log file (/var/log/system.log).
Louis Gerbarg
+2  A: 
#include <unistd.h>
sleep(3); // or usleep(3000000);
Peter N Lewis