Perform Time Consuming Task in Background Thread in Swift

In this short Swift code example we will learn how to perform a time consuming task in background thread in Swift, to keep mobile app responsive to user actions while the task is being executed and how to update UI from an async task once background task has completed.

  • Do some task in background thread
  • Keep mobile app responsive to user actions while the background task is being executed
  • Update user interface from async task once the background task is completed
 .userInitiated).async {
            // Do some time consuming task in this background thread
            // Mobile app will remain to be responsive to user actions
            print("Performing time consuming task in this background thread")
           DispatchQueue.main.async {
                // Task consuming task has completed
                // Update UI from this block of code
                print("Time consuming task has completed. From here we are allowed to update user interface.")

Learn iOS Development with these Video Courses

How to Make a Freaking iPhone App - iOS 10 and Swift 3
icon icon

iOS 10 & Swift 3: From Beginner to Paid Professional

The Complete iOS 10 Developer Course - Build 21 Apps

Swift 3 - Learn to Code with Apple's New Language
icon icon

Learn How to Build Mobile Apps for iOS with Swift, PHP and MySQL
icon icon

Follow me on one of your favourite social networks to learn about new video tutorials and code examples:

Twitter: @SwiftVideoBlog
Google Plus:
Facebook: Swift Developer Blog on Facebook