What's the equivalent of Java's Thread.sleep() in Objective-C/Cocoa?

Solution 1:

Yes, there's +[NSThread sleepForTimeInterval:]

(Just so you know for future questions, Objective-C is the language itself; the library of objects (one of them at least) is Cocoa.)

Solution 2:

Sleeping for one second in Java:

Thread.sleep(1000);

Sleeping for one second in Objective C:

[NSThread sleepForTimeInterval:1.0f];

Solution 3:

Why are you sleeping? When you sleep, you are blocking the UI and also any background URL loading not in other threads (using the NSURL asynchronous methods still operates on the current thread).

Chances are what you really want is performSelector:withObject:AfterDelay. That's a method on NSObject you can use to call a method at some pre-determined interval later - it schedules a call that will be performed at a later time, but all of the other stuff the thread handles (like UI and data loads) will still continue.