How to Wait in Objective-C and Swift

帅比萌擦擦* 提交于 2019-12-02 16:33:46
Michał Zygar

You can use

[self performSelector:@selector(changeText:) withObject:text afterDelay:2.0];

or if you want to display it periodically, check the NSTimer class.

I know I am late to this party. But I found people haven't mention thread sleep. If you are using GCD to call that function. You can use :

[NSThread sleepForTimeInterval:2.0f];   

to delay the thread for 2 seconds.

[self changeText: @"A text"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Here your non-main thread.
        [NSThread sleepForTimeInterval:2.0f];   
        dispatch_async(dispatch_get_main_queue(), ^{
            //Here you returns to main thread.
            [self changeText: @"Another text"];
        });
    });

Edit 2 (Feb 2015):

I think the NSTimer is a great solution. My solution just giving another option to achieve the goal of NSTimer.

Please read: How do I use NSTimer?

[NSTimer scheduledTimerWithTimeInterval:2.0
                                 target:self
                               selector:@selector(doSomethingWhenTimeIsUp:)
                               userInfo:nil
                                repeats:NO];

In the class, you need this method:

- (void) doSomethingWhenTimeIsUp:(NSTimer*)t {

        // YES! Do something here!!

}

Edit 3 (May 2016):

In Swift 2.0, you can use this way:

 NSTimer.scheduledTimerWithTimeInterval(2.0, 
                                         target: self, 
                                       selector: "doSomethingWhenTimeIsUp:", 
                                       userInfo: nil, 
                                        repeats: false)

It creates an NSTimer's entity and adds the timer automatically to the NSRunLoop associated with the NSThread in which the timer is created.

Edit 4 (Jun 2016):

In Swift 2.2, the way to invoke select is:

#selector(doSomethingWhenTimeIsUp(_:))

So, it is something like:

  NSTimer.scheduledTimerWithTimeInterval(2.0,
                                      target: self,
                                      selector: #selector(doSomethingWhenTimeIsUp()),
                                      userInfo: nil,
                                      repeats: false)

Edit 5 (Oct 2016):

In Swift 3, the way to invoke select is:

#selector(doSomethingWhenTimeIsUp)

So, it is something like:

        Timer.scheduledTimer(timeInterval: 2.0,
                             target: self,
                             selector:#selector(doSomethingWhenTimeIsUp),
                             userInfo: nil,
                             repeats: false)

Then, the func should looks like this:

        @objc private func doSomethingWhenTimeIsUp(){
        // Do something when time is up
        }

Edit 6 (May 2018):

In Swift 4, we can do as below way.

    let delaySeconds = 2.0
    DispatchQueue.main.asyncAfter(deadline: .now() + delaySeconds) {
        doSomethingWhenTimeIsUp()
    }  

Then, the func should looks like this:

    private func doSomethingWhenTimeIsUp(){
        // Do something when time is up
        }

Grand Central Dispatch has a helper function dispatch_after() for performing operations after a delay that can be quite helpful. You can specify the amount of time to wait before execution, and the dispatch_queue_t instance to run on. You can use dispatch_get_main_queue() to execute on the main (UI) thread.

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    // do something
});

In Swift 3, the above example can be written as:

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
    // do something
}

You can use NSTimer, like so -

[NSTimer scheduledTimerWithTimeInterval:0.5 
                                 target:self 
                               selector:@selector(updateLabel:) 
                               userInfo:nil 
                                repeats:YES];

Define another method updateLabel and do the updation there. Define your timeInterval to suite your needs...

Also setting repeats to YES makes sure that this selector is executed every 0.5 seconds (in the above case).

You can accomplish this with a timer, e.g.

   NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(eventToFire:) userInfo:nil repeats:YES]; // Fire every 4 seconds.

   ...

   - (void)eventToFire:(NSTimer*)timer {
      // Do Something
   }

This is because the view isn't updated until the end of the runloop. Instead of using sleeps try using NSTimer to set a specific time to update the view.

You need to use a timer. Using sleep will halt your entire program. Check NSTimer

The follow method is good.

You can replace your

sleep(1);

with

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC));

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!