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

后端 未结 6 433
野的像风
野的像风 2020-12-23 15:45

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?

6条回答
  •  庸人自扰
    2020-12-23 16:18

    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];
        }
    }
    

提交回复
热议问题