在 iOS 上帮助多线程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4360591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Help with multi-threading on iOS?
提问by Moshe
I have an application which utilizes OpenEars and the Flite library. The problem is that the Flite library is resource intensive and it's freezing up my app. I suspect that running Flite on a background thread will fix things, but I have noidea how to do so.
我有一个使用 OpenEars 和 Flite 库的应用程序。问题是 Flite 库是资源密集型的,它冻结了我的应用程序。我怀疑在后台线程上运行 Flite 会解决问题,但我不知道该怎么做。
That said, how do I implement a background thread in iOS?
也就是说,如何在 iOS 中实现后台线程?
I'd appreciate if anyone can point me to some tutorials, share some sample code, or any general advice that would help me solve this problem.
如果有人能指点我一些教程、分享一些示例代码或任何可以帮助我解决此问题的一般建议,我将不胜感激。
回答by zoul
The Concurrency Programming Guideby Apple is a nice reading. Concurrent programming is not something you might want to pick up by copying some sample code from the web and hacking until you are happy. It's good to know the options and principles to save yourself from trouble.
Apple的Concurrency Programming Guide是一本不错的读物。并发编程不是您可能想要通过从网络复制一些示例代码并进行黑客攻击直到您满意为止的东西。了解可以避免麻烦的选项和原则是很好的。
Revisiting the answer after some time, nowadays you almost can't go wrong using Grand Central Dispatch. Running a task in background looks like this:
一段时间后重新审视答案,现在使用 Grand Central Dispatch 几乎不会出错。在后台运行任务如下所示:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self doSomeLongTask]; // 1
dispatch_async(dispatch_get_main_queue(), ^{
[self longTaskDidFinish]; // 2
});
});
The long task (1) will run on some background thread and there's no catch that I am aware of, ie. there's already an autorelease pool in that thread, you don't have to care about run loops etc. After the task finishes the code calls -longTaskDidFinish
on the main thread (2), so that you can update UI or whatever else. This is an often used idiom.
长任务 (1) 将在某个后台线程上运行,并且我不知道有什么问题,即。该线程中已经有一个自动释放池,您不必关心运行循环等。任务完成-longTaskDidFinish
后主线程 (2) 上的代码调用,以便您可以更新 UI 或其他任何内容。这是一个经常使用的成语。
回答by EDUARDO CAPANEMA
Maybe the best thing to do is this tutorial from Apple. I read it carefully (10-20 minutes) and “threaded” all my application! Excellent!
也许最好的办法是来自 Apple 的这个教程。我仔细阅读了它(10-20 分钟)并“线程化”了我所有的应用程序!优秀!
回答by Mark Moeykens
Swift 3
斯威夫特 3
DispatchQueue.global(qos: .userInteractive).async {
// Code to run on background thread
// Switch to the main UI thread to display any results needed
DispatchQueue.main.async {
// Run code on main UI thread here
}
}
The qos
parameter stands for "Quality of Service". Think of it like a priority to give your background thread:
该qos
参数代表“服务质量”。将其视为优先考虑给予您的后台线程:
.userInteractive
(highest priority).userInitiated
(when you can spare a few seconds).utility
(when you can spare a few seconds to few minutes).background
(lowest priority - minutes/hours to spare)
.userInteractive
(最高优先级).userInitiated
(当您可以抽出几秒钟时).utility
(当您可以抽出几秒钟到几分钟时).background
(最低优先级 - 剩余分钟/小时)