Understanding threads

You've seen the term thread a couple of times now, but you never explored them; you never really learned what a thread is or how a thread works. This section aims to make the subject of threading a lot clearer to you so you can fully understand what a thread is, and why threads are such a vital part of building apps.

A good way to think of a thread is as a stack of instructions. In iOS, your app typically starts off with a single thread  the main thread. This thread is also known as the UI thread. It's called the UI thread because the main thread is where all of the user interface elements are configured, rendered, and pushed to the screen. Anything that is related to the user interface must be executed on the main thread, so if you think of a thread as a stack of instructions that are run one by one, it's easy to see why it's so important that the main thread doesn't get stuck performing a very slow instruction:

The preceding image shows a timeline where all code is executed on the main thread. Notice how the interface can't be updated until the fetch data and parse JSON instructions are completed. Also, note that fetching data takes a lot longer than displaying the interface or handling a tap. During the fetch data instruction, the app is unable to update any user interface elements or process any gestures or taps. This means that the app is practically frozen until the data is fetched and parsed.

A good, responsive application can't afford to wait for slow instructions. The interface should always respond to user input; if this isn't the case, the app feels slow, buggy, choppy, and just all-around bad.

If an app uses multiple threads, it can run various instruction stacks at the same time. Each stack is called a thread, and specific instructions can be performed on a different thread to ensure that the main thread remains responsive:

This second figure shows a more desirable scenario. The main thread only handles tasks that are related to the user interface, such as taps, animations, and scrolling. The background thread takes care of the tasks that are not related to the user interface and could potentially take a while to finish. By removing these instructions from the main thread and placing them on a different thread, like iOS does by default for networking, you can ensure that your app remains responsive, even if the network requests take several seconds to finish or never finish at all. Your app can utilize a large number of threads for different tasks.

The number of threads isn't infinite, so make sure that you optimize your code as much as possible to avoid locking up several threads with slow code.

In Chapter 27, Discovering Bottlenecks with Instruments, you used Instruments to locate slow code in a sample app. The slow code was an instruction on the main thread that took a very long time to complete, resulting in a frozen interface. However, threading would not have solved this particular issue. The slow code you discovered calculated the layout for a collection view. A collection view can't be rendered without calculating the layout first, so this is a scenario where it's essential to make sure that you write optimized code, instead of relying on threads for anything that's slow.

Now that you have a better understanding of threads and how they can be used, let's have a look at how to offload tasks to different threads.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.251.22