25

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.

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.

Moshe
  • 57,511
  • 78
  • 272
  • 425
  • have you been able to streamline OpenEars' flite library through queues or threads? – Rasman Dec 15 '11 at 20:04
  • @Rasman - I haven't tried, honestly, been really busy. I'm working on multithreading on another project so I may revisit this soon. – Moshe Dec 16 '11 at 03:03
  • ok, as a follow-up, I just build my app with dispatch queues and it seems to work well for now. It was all actually easier then I expected... – Rasman Dec 16 '11 at 20:35

3 Answers3

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.


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
    });
});

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.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • What Zoul said; threading is hard. Even if your code appears to be working when run, it still may be very very wrong. – bbum Dec 05 '10 at 19:34
  • Tell me how can i get on this thread from my method doSOmeLongTask ? For example i do like you have posted. and inside my method "doSomeLongTask" i need do some stuff on end this thread – Shial Mar 26 '14 at 10:52
  • What would go in `[self.longTaskDidFinish]`. In my app at the end of `doSomeLongTask` I change a variable to indicate that a button can be pressed. Would I change the variable in `[self longTaskDidFinish]` instead of changing it right after I do `[self doSomeLongTask]` – Foobar Sep 02 '16 at 13:04
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!

zoul
  • 102,279
  • 44
  • 260
  • 354
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:

  • .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)
Community
  • 1
  • 1
Mark Moeykens
  • 15,915
  • 6
  • 63
  • 62