GCD Introduction to concurrency and parallelism in iOS
Posted By : Anmol Aggarwal | 31-Aug-2019
GCD
Introduction to concurrency and parallelism in iOS
What is GCD?
GCD stands for Grand Central Dispatch. In swift we have very less options when it comes to asynchronous operations, these are that part of the code which we don’t want to execute immediately as in our main execution flow but we rather executed along some other block of code or after completion of certain tasks.
GCD is one such option or we can say it is a “low-level API for managing concurrent operations” used to make your applications run smoothly and provide great responsiveness improving UX for the users using your application. It manages multiple tasks that are needed to be done at same time.
We all have seen code like this:
DispatchQueue.main.async {
// Perform your async code here
}
DispatchQueue
DispatchQueue API works like a company having various low as well high levels of workers, so, that it can manage both heavy and light tasks. We can use DispatchQueue as explained below:
1.Perform network task with UI updates:
- Global Queue – We use it to perform non-UI related work in the background so that are application doesn’t seem to freeze.
- Main Queue - We use it to update the UI after task on a concurrent queue is successfully completed.
2. List of DispatchQueue Priority:
.userInteractive
.userInitiated
.default
.utility
.background
.unspecified
3.Perform background Quality of Service (QOS) task:
DispatchQueue.global(qos: .background).async { // Call your background task DispatchQueue.main.async { // UI Updates here for task complete. } }
4.DispatchQueue with delay:
let deadlineTime = DispatchTime.now() + .seconds(1) DispatchQueue.main.asyncAfter(deadline: deadlineTime) { //Perform code here }
5. Dispatch Groups:
let dispatchGroup = DispatchGroup() dispatchGroup.enter() loadUseractivities { dispatchGroup.leave() } dispatchGroup.enter() loaduserComments { dispatchGroup.leave() } dispatchGroup.notify(queue: .main) { print("All the Tasks have been Completed!!") }
Conclusion
Finally we are able to say Grand central dispatch is that the powerful API for multitasking with sync and async programming in iOS. Also, GCD is a powerful tool, and while working with asynchronous closures onto different queues barely scratches the surface of what the framework is capable of — it very well may be a great method to begin working with asynchronous operations and concurrency using an extremely lightweight syntax.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Anmol Aggarwal
Anmol is in mobile team, She is hard working developer and dedicated towards her work.