gpt4 book ai didi

iphone - NSThreads、NSOperations 和 PerformSelector 之间的区别

转载 作者:行者123 更新时间:2023-12-03 18:41:10 25 4
gpt4 key购买 nike

我想问一些有关 iPhone 开发的简单但重要的问题。如果我们必须在后台执行任务,并且当后台任务完成时,我们将更新 UI,为此我们可以使用 NSThreads、NSOperations 或 (performSelector)performSelectorInBackgroundThread。所有这些之间有什么区别以及它们将如何影响我的应用程序性能。

还有一件事,下面这两个语句有什么区别:-

[self getData];

[self performSelector:@selector(getData)];

Please explain as i dont know the difference between all these things.

最佳答案

实际上差别不大

[self getData];

并且

[self performSelector:@selector(getData)];

唯一的区别是,当您调用 [self getData] 时,编译器可以确定您要将 getData 消息发送到类 [self class] 的对象。如果它找不到任何先前声明的方法原型(prototype),则会出现警告。

第一行和第二行将被翻译为

objc_msgsend(self, _cmd)

performSelector: 当您想在运行时执行某些操作时(例如,您在运行时确定要发送给对象的确切消息),这确实很酷。或者这里是“现实生活”中的一个例子:UIButton 有方法

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

因此它将操作存储在其内部的某个位置,并且当有适当的控制事件时它会调用:

[target performSelector: action];

NSOperation 是用线程包装工作的好方法。 NSThread 只是 pthreads 的包装。

因此,您的应用程序性能并不真正取决于您使用线程的方式,但是使用 NSOperation 而不是 pthreads 更容易做到这一点。

关于iphone - NSThreads、NSOperations 和 PerformSelector 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5176685/

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