如何在一行代码中编写`performSelectorInBackground:afterDelay:`?

时间:2012-01-11 12:07:13

标签: objective-c multithreading selector delay

performSelectorInBackground:中有方法performSelector: afterDelay:NSObject。如何在一行代码中组合它们? 也许以某种方式NSTimer

2 个答案:

答案 0 :(得分:13)

您是否考虑过使用块?

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^(void){
    //your code here
});

另外,如果写作太多,那么已经有了它的片段,开始输入" dispatch_after"而且你会看到它。

答案 1 :(得分:-1)

NSTimer

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