gpt4 book ai didi

objective-c - 好奇号: executing code in another thread inside the same method?

转载 作者:行者123 更新时间:2023-12-03 17:57:44 25 4
gpt4 key购买 nike

我发布此内容是因为我对某些事情感到好奇。例如,让我们看一下这个示例代码(仅作为示例):

    -(void)doSomething{
for (int i=0;i<10000000000;i++){
nslog(@"%i", int);
}
nslog(@"i'm done!");
}

有没有办法执行for loop使用相同的方法( doSomething )但在另一个线程中,不需要执行类似 NSOperationQueue 的操作(@selector)?
并且NSLog必须在for loop之后执行。完成了。
如果有的话,示例代码将不胜感激!
谢谢。

最佳答案

是的,您可以将 GCD 与 dispatch_async 结合使用。但是在所有迭代完成后,im done NSLog 不会被执行。这是没有意义的,因为它必须在主(主)线程上等待后台(迭代)线程完成。

如果您不想阻止 UI 的主要方法,则必须在后台线程中执行整个方法。

关于objective-c - 好奇号: executing code in another thread inside the same method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10825385/

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