Threading in iOS can be complicated to know if you are coming from other platforms or are just a beginner at swift. The following are some tips to get you off on the right foot with Threading by using GCD.
First a precursor, threading is all about running how work is prioritized in your app. creating your code execute faster is great, but what matters more how fast the user perceives your app to be.
Your goal as a developer is to compute anything that the user can observe and interact with. It creates your app feel faster and edgy. Don’t make the user wait for something to fill that they don’t observe or concern about.
Simply utilize the Main Thread for updating views
This is the easiest way to avoid issues. The vital idea is creating sure that all views and interface elements are not blocked by other items in the Main Queue. A good example of this:
By only doing UI related work on the Main Thread you are ensuring that the user will not be blocked from loading a view or exiting a view. evade functions that have side effects that load data, image, etc on the Main Thread.
Identify the different GCD Quality of Service (QoS) types
There are some dissimilar priorities to execute work in iOS. The higher qualities are prioritized first and are knob right away, while the lower qualities of service are knobbed when the system frees up some resources.
Apple’s Documentation on different QoS
Building your own queue with quality of service is really simple. You just have to provide a label and a QoS.
Know what thread you are on
To recognize what the current thread is at any provide mark in your code, Thread.current.
Following this you can observe exactly what priority the function is operating on. You can also verify if you are particularly on the main thread with Thread.current.isMainThread .
Almost always use async
This is one of the simplest ways to create a mistake when threading, but is also simply avoidable. There are profits to using sync in your code, and some particular cases need it, but for someone just beginning iOS development perhaps better to just evade it.
Also evade using the sync on .userInteractive Queue because it is at the similar priority level of the main queue.