帮助iOS上的多线程? [英] Help with multi-threading on iOS?

查看:84
本文介绍了帮助iOS上的多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用OpenEars和Flite库的应用程序。问题是Flite库是资源密集型的,它冻结了我的应用程序。我怀疑在后台线程上运行Flite会解决问题,但我没有想法如何做到这一点。

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.

那就是说, 如何在iOS中实现后台线程

That said, how do I implement a background thread in 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.

推荐答案

并发编程指南来自Apple的是一个很好的阅读。通过从Web上复制一些示例代码并进行黑客攻击直到您满意为止,并发编程并不是您想要的。很高兴知道避免麻烦的选项和原则。

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.

一段时间之后重新回答答案,现在你差不多了使用Grand Central Dispatch不会出错。在后台运行任务如下所示:

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:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self doSomeLongTask]; // 1
    dispatch_async(dispatch_get_main_queue(), ^{
        [self longTaskDidFinish]; // 2
    });
});

长任务(1)将在某个后台线程上运行,并且没有任何我知道的问题,即。在该线程中已经存在自动释放池,您不必关心运行循环等。任务完成后,代码在主线程(2)上调用 -longTaskDidFinish ,以便您可以更新UI或其他任何内容。这是一种常用的习语。

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.

这篇关于帮助iOS上的多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆