Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call method with delay and in background thread

I have a method what I want to call after -viewDidLoad and in background thread. Is there way to combine this two methods:

[self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]

and

[self performSelectorInBackground:(SEL) withObject:(id)]?

like image 684
RomanHouse Avatar asked Jun 22 '12 18:06

RomanHouse


1 Answers

Grand Central Dispatch has dispatch_after() which will execute a block after a specified time on a specified queue. If you create a background queue, you will have the functionality you desire.

dispatch_queue_t myBackgroundQ = dispatch_queue_create("com.romanHouse.backgroundDelay", NULL);
// Could also get a global queue; in this case, don't release it below.
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC);
dispatch_after(delay, myBackgroundQ, ^(void){
    [self delayedMethodWithObject:someObject];
});
dispatch_release(myBackgroundQ);
like image 99
jscs Avatar answered Sep 21 '22 03:09

jscs