Help with multi-threading on iOS?
Asked Answered
C

3

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.

Chatelaine answered 5/12, 2010 at 19:5 Comment(3)
have you been able to streamline OpenEars' flite library through queues or threads?Celinecelinka
@Celinecelinka - I haven't tried, honestly, been really busy. I'm working on multithreading on another project so I may revisit this soon.Chatelaine
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...Celinecelinka
V
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.

Volans answered 5/12, 2010 at 19:8 Comment(3)
What Zoul said; threading is hard. Even if your code appears to be working when run, it still may be very very wrong.Electroanalysis
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 threadIncidence
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]Balneal
P
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!

Parlous answered 3/3, 2012 at 2:54 Comment(0)
G
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)
Geoponic answered 16/3, 2017 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.