GCD – Don’t freeze your app anymore 😎

Have you ever seeing that your application is freezing or feels like laggy? something like this.

Application completely freeze

I have to tell you that this isn’t a break point, the application is complete freeze and i’m not able to hit another button and i’ll explain you why.

The button just run the following code

This functions just print a lot of workers, and this is the reason that our application is freeze, now the for is very simple but imagine that you have to do a lot of work inside the function of that button. How we can handle that?

DisptachQueues and WorkItems

First we need to know how to write code which be executed in background or not in the main thread. since iOS 10 swift now handle all the background code with DispatchQueues and WorkItems this could sound like a hard concepts, but to be honest those are very simple imagine a queue of elements and every element is a bunch of code which will be executed serial or concurrent depends of the properties of the Queue

Imagine every block in the follow imagine like a work item, and ever line as a dispatch queue, this is the simplest explanation. As you can see the Main thread could be seen as a serial dispatch queue, that’s why our application is frozen when we have a very large worload.

dispatch queues concurrent and serial

Also is important that note that every queue has different priorities like the following


User-interactive has more priority than User-instantiated and so, you need to be careful about what type of quality of service your dispatch queue needs to be.

Now in order to understand what is going on with this queue we can do the following test in playground.

Then you’ll see something like this in the console:

Queue priority in playground

As you can see, the blue dots are dominating, this is because the 🔵 blue dot is printed in a queue with more priority than the 🔴 red dot, the count is evident also, the blue dot has printed 115 dots and the red ones only 41.

Cool now i know how the dispatch queues works, now how can i avoid the freeze?

In order to avoid the freeze we can put all the workload in a queue, and also add the ability to cancel the process, because maybe the user wants to cancel the process and go back in your app.

Ok, maybe this looks scary at the first time, but let me explain you, remember that i told you that every block in the queue is a work item? well you add work items or blocks of code to your queue just like i’m doing here in the line 14 also if you are curios you’ll see that there is a reference inside the work item to itself to ask every time in the for loop if the work item was canceled outside, this is a ugly way to cancel the block inside the queue, but you need to know that if you don’t cancel the work before this is executed you can cancel in the middle of the process, you can do something like this.

Also you can check if the work item was cancelled in some strategic points when you are not doing some dangerous work like writing in your database for example.

Putting all together you’ll see something like this:

Stop heavy workload with queues

Where is the code?

If you want to run yourself you can download my repo of this example! Happy coding! 🍏 🚧