gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:13:40 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

关于iphone - 如何在后台线程中延迟调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11162499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com