这是你的问题:
for(int i = 0; i < 1000; i++) {
[self performSelectorInBackground:@selector(someHardComputations) withObject:nil];
[self performSelectorInBackground:@selector(someHardComputations) withObject:nil];
}
(虽然我意识到someHardComputations
这只是一个示例,但我假设您的实际代码仍然使用performSelectorInBackground:
.)
这会创建不合理数量的后台线程(2000)。它并没有让事情变得更快(你仍然只有一定数量的核心)。它只是极大地增加了线程争用并干扰了主线程。在现代程序中没有充分的理由使用performSelectorInBackground:
.
GCD (dispatch_queue) 是你想要的工具。特别是,您希望将此工作放在具有 QoS 类 UTILITY(也称为 LOW 优先级)的队列上,这样它就不会与您的主队列竞争。
有关如何使用 GCD 的介绍,请参阅并发编程指南。我不能给你一个确切的解决方案,因为究竟如何实现这个在很大程度上取决于someHardComputations
.