Threading in iOS Applications: The Basics

threading in iOS applications iOS Development

Threading in iOS Applications: The Basics

If your new to mobile application development, one topic you should learn early on, is the concept of threading. Threading is a computer science term that handles the order of execution of tasks on the CPU. Threads are usually handled by the operating system. However there might be some tasks that can only be carried out on specialized threads created by the OS. To put things in context, consider the following example. Lets say I create an iOS application that uses a REST API to fetch data about upcoming concerts using location data. When you launch this application, the app freezes, takes a long time to load, and you see a white screen for more than 3 seconds. This is a common example of an app thats not optimized for the use of threads. As a result, you might  receive complaints from your users saying that the app is slow or they delete your application all together. After a while, you decide to make some changes.


Upon inspection of your code, you find that your REST call is causing this slow down inside the ViewDidLoad method. you have to remember that in this case your code is being executed line by line. This means when your REST call is carried out, the CPU must finish getting your data before displaying it on screen. After reading this, you may be asking why does this happen? This happens because by default your application business logic and UI are loaded on the same thread called the main thread. Quick operations like simple math and string manipulation are perfectly fine on the main thread, but time consuming tasks like REST calls are not the best option. So how do we fix this? The simplest thing we could do is move this time consuming task to what is called the Background Thread. The Background Thread is a thread designed for complex operations. In return you leave the Main Thread open for UI changes. Here is some example code on how to make this happen.


Sample Code 


Now lets break this code down. DispatchQOS is a quality of service object that decides what kind of priority is required for our task, to run in the background thread. In this case there is many options to choose from. I’m only going to focus on two levels. However if your interested, Apple’s documentation explains all of the levels in detail. At a very basic level, background has the lowest priority by default. This option got you to this post. On the other hand, UserInteractive is the highest. I recommend combining this with the async method so you do not have to worry about other tasks waiting to be ran. Please keep in mind that the reloadData method will continue to run on the main thread because it has to deal with updating your UI. Using these steps will help you create better and perhaps faster iOS apps. If you liked this post, you should definitely check out my last iOS related post on UIAlertController.






Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: