-2

I have 6 functions. I would like to execute 3 of these functions in the background (asynchronous?), in Xcode for Swift 2. Can you help me, how can I execute this 3 functions without a "freeze" in the UI? Thank you so much!

Abizern
  • 146,289
  • 39
  • 203
  • 257
Andi
  • 15
  • 1
  • 3
  • 2
    Apple's [Concurrency Programming Guide](https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html) might be a good start ... – Martin R Nov 12 '16 at 21:50
  • 1
    Dispatch in Swift 3 is MUCH nicer. You really shouldn't be using Swift 2 anymore. – Alexander Nov 13 '16 at 00:42

1 Answers1

0

Martin R's comment is spot on, you should read Apple's concurrency programming guide. But here's one method you might use:

let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)
dispatch_async(queue) {
    doSomethingReallySlowAndTedious()
}

Be careful, though. It's especially easy to introduce bugs in your code by just copy-pasting concurrency code. Make sure you know what each function and parameter does.

(Also, consider the worst cases. Can the slow functions get stuck forever? What if the user wants to quit before the slow functions finish?)

jjs
  • 1,338
  • 7
  • 19
  • Alexander Momchliov added a good comment to your question. Swift 3 now has much better syntax for async stuff, so you don't have to use the C-like calls I used in my answer's example. There's some good discussion at http://stackoverflow.com/questions/37805885/how-to-create-dispatch-queue-in-swift-3/37806522 – jjs Nov 14 '16 at 10:53