Handling Background Tasks
In this section of the tutorial, we will find out the ways to handle the background tasks in the iOS application. Here, we will improve one of the applications that we built in the previous sections of this tutorial, which is the Artist Project that we built to show the Artist data to the user.
Now, we will use multithreading to improve the performance of the Artist Project. In Artist Project, we have performed image caching in the viewDidLoad() on the main thread. However, this could be performed as the background task asynchronously. Here, we must notice that all the UI updating tasks will need to be on the main thread always.
The following code is used to perform image caching at the background using the global dispatch queue.
This will let the viewDidLoad(: ) to end before caching the images and reloading the table view. Here, we must notice that the table view is reloaded on the main thread, and all the UI updates must always happen on the main thread.
The modified ViewController.swift is given below.
Since we have used the print statement in the code, let's look at the console output given below.
"end of view did load" ArtistProject.ArtistResponseModel "performing image caching" "reloading tableview"