如何在延迟和后台线程中调用方法

时间:2012-06-22 18:58:39

标签: iphone objective-c ios

我有一个方法,我想在-viewDidLoad之后和后台线程中调用。有没有办法结合这两种方法:

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

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

4 个答案:

答案 0 :(得分:22)

Grand Central Dispatch有dispatch_after(),它将在指定队列上的指定时间后执行一个块。如果您创建后台队列,您将拥有所需的功能。

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);

答案 1 :(得分:7)

尝试以下方法:

// Run in the background, on the default priority queue
dispatch_async( dispatch_get_global_queue(0, 0), ^{
    [self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]
});

未经过测试的代码

请注意,您的选择器/方法不得使用UIKit(因此不要更新UI)或访问UIKit属性(如框架),这样您的选择器可能需要将工作重新启动回主线程。 e.g。

(id)SomeMethod:UsingParams: {

    // Do some work but the results

    // Run in the background, on the main queue
    dispatch_async(dispatch_get_main_queue(), ^{
        // Do something UIKit related
    });
}

答案 2 :(得分:4)

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

在正在调用它的线程上执行选择器。因此,当你从后台线程调用它时,它将在那里运行......

答案 3 :(得分:2)

您可以按照示例执行此操作:

dispatch_time_t delay = dispatch_time( DISPATCH_TIME_NOW, <delay in seconds> * NSEC_PER_SEC );
dispatch_after( delay, dispatch_get_main_queue(), ^{
    [self performSelectorInBackground: <sel> withObject: <obj>]
});

不知何故是混合解决方案。坚持使用完整的GCD方法会更好。