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

IosObjective CCocoa TouchSleep

Ios Problem Overview


In Java you can suspend the current thread's execution for an amount of time using Thread.sleep(). Is there something like this in Objective-C?

Ios Solutions


Solution 1 - Ios

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 - Ios

Sleeping for one second in Java:

Thread.sleep(1000);

Sleeping for one second in Objective C:

[NSThread sleepForTimeInterval:1.0f];

Solution 3 - Ios

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.

Solution 4 - Ios

Of course, you could also use the standard Unix sleep() and usleep() calls, too. (If writing Cocoa, I'd stay with the [NSThread sleepForTimeInterval:], however.)

Solution 5 - Ios

If you use NSThread sleepForTimeInterval(commented code) to sleep, fetching data will be blocked, but +[NSThread sleepForTimeInterval:] (checkLoad method) will not block fetching data.

My example code as below:

- (void)viewDidAppear:(BOOL)animated
{
//....
//show loader view
[HUD showUIBlockingIndicatorWithText:@"Fetching JSON data"];
//    while (_loans == nil || _loans.count == 0)
//    {
//        [NSThread sleepForTimeInterval:1.0f];
//        [self reloadLoansFormApi];
//        NSLog(@"sleep ");
//    }
[self performSelector:@selector(checkLoad) withObject:self afterDelay:1.0f];
}

-(void) checkLoad
{
    [self reloadLoansFormApi];
    if (_loans == nil || _loans.count == 0)
    {
        [self performSelector:@selector(checkLoad) withObject:self afterDelay:1.0f];
    } else
    {
        NSLog(@"size %d", _loans.count);
        [self.tableView reloadData];
        //hide the loader view
        [HUD hideUIBlockingIndicator];
    }
}

Solution 6 - Ios

usleep() can also be used as ive used this to pause the current thread at times

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleviView Question on Stackoverflow
Solution 1 - IossmorganView Answer on Stackoverflow
Solution 2 - IosRok StrnišaView Answer on Stackoverflow
Solution 3 - IosKendall Helmstetter GelnerView Answer on Stackoverflow
Solution 4 - IosBill HeymanView Answer on Stackoverflow
Solution 5 - Ioswu liangView Answer on Stackoverflow
Solution 6 - Iosj2emanueView Answer on Stackoverflow