iOS上的多线程有帮助吗?

时间:2021-11-25 20:59:24

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 no idea how to do so.

我有一个应用程序,它利用了openear和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.

如果有人能给我指出一些教程,分享一些示例代码,或者任何能帮助我解决这个问题的一般建议,我将不胜感激。

3 个解决方案

#1


45  

The Concurrency Programming Guide by 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.

苹果的并发编程指南是一个很好的读物。通过从web上复制一些示例代码并进行黑客攻击直到您满意为止,您可能不希望采用并发编程。知道如何从麻烦中拯救自己的选择和原则是很好的。


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)将运行在某个后台线程上,我没有注意到捕获。在这个线程中已经有一个autorelease池,您不必关心运行循环等。在任务完成后,在主线程(2)上调用-longTaskDidFinish的代码,这样您就可以更新UI或其他东西。这是一个常用的成语。

#2


6  

Maybe the best thing to do is this tutorial from Apple. I read it carefully (10-20 minutes) and “threaded” all my application! Excellent!

也许最好的方法就是苹果的这个教程。我仔细阅读(10-20分钟)和“线程”所有我的应用!太好了!

#3


1  

Swift 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)
  • .userInteractive(最高优先级)
  • .userInitiated (when you can spare a few seconds)
  • . userinitiativesuite(您可以抽出几秒钟的时间)
  • .utility (when you can spare a few seconds to few minutes)
  • .utility运用(当你能抽出几秒钟到几分钟的时间时)
  • .background (lowest priority - minutes/hours to spare)
  • .背景(最低优先级-分钟/小时)

#1


45  

The Concurrency Programming Guide by 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.

苹果的并发编程指南是一个很好的读物。通过从web上复制一些示例代码并进行黑客攻击直到您满意为止,您可能不希望采用并发编程。知道如何从麻烦中拯救自己的选择和原则是很好的。


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)将运行在某个后台线程上,我没有注意到捕获。在这个线程中已经有一个autorelease池,您不必关心运行循环等。在任务完成后,在主线程(2)上调用-longTaskDidFinish的代码,这样您就可以更新UI或其他东西。这是一个常用的成语。

#2


6  

Maybe the best thing to do is this tutorial from Apple. I read it carefully (10-20 minutes) and “threaded” all my application! Excellent!

也许最好的方法就是苹果的这个教程。我仔细阅读(10-20分钟)和“线程”所有我的应用!太好了!

#3


1  

Swift 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)
  • .userInteractive(最高优先级)
  • .userInitiated (when you can spare a few seconds)
  • . userinitiativesuite(您可以抽出几秒钟的时间)
  • .utility (when you can spare a few seconds to few minutes)
  • .utility运用(当你能抽出几秒钟到几分钟的时间时)
  • .background (lowest priority - minutes/hours to spare)
  • .背景(最低优先级-分钟/小时)