Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute method after delay not using NSThread or sleep() - is there another option?

Is there a way in iOS4 to call a method after a delay using something else other than NSThread or blocking the UI using sleep()?

like image 387
Slee Avatar asked Jul 12 '11 14:07

Slee


4 Answers

    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // code to be executed on main thread.If you want to run in another thread, create other queue
    });
like image 99
Marcelo Alves Avatar answered Nov 19 '22 15:11

Marcelo Alves


- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

Of NSObject will do this for you.

Additionally, you can create an NSTimer and have it perform a callback to some target and selector after a given amount of time.

like image 42
MarkPowell Avatar answered Nov 19 '22 16:11

MarkPowell


You can using NSTimer. (for example timerWithTimeInterval:target:selector:userInfo:repeats)

like image 3
MByD Avatar answered Nov 19 '22 14:11

MByD


[self performSelector:@selector(methodName) withObject:nil afterDelay:2.0];
like image 3
Vijay-Apple-Dev.blogspot.com Avatar answered Nov 19 '22 16:11

Vijay-Apple-Dev.blogspot.com