If you are IOS developer you can run into a problem when your app download a lot of image from URL maybe your app is crashed or you can't interact with the rest of the application, when I interviewed for a job a few months ago, I also got this question from my interviewer
He assumed that there are 3 images from URL when user click on the update button all of them will be updated at the same time so let’s do it a demo to know what happens when your application run without multithreading
Inside of my project I need some element into ViewController,
As you can see we have 2 images and a button to update our image
Inside of ViewController, we need to hook up all of them with @IBOutlet and @IBAction it should look like this
As you can see when user click on the update button to download two images from URL, you can't interact with the rest of the application you can’t do anything with the update button
So how to deal with that problem “Apple provides GCD can help us deal with that problem” Grand Central Dispatch or GCD is a low-level API for managing concurrent operations. It will make your application smooth and more responsive. Also helps for improving application performance. Sometimes we are trying to perform multiple tasks at the same time that time most of the developer-facing application hang or freezing issue this is the common issue. That’s why we are using GCD to manage multiple tasks at the same time.
So how to use GDC inside of our project, we’re going to create a new function look like this
And we need to create another function can help us load two images from URL
After that we can create a new function look like this, this function can notify when we get image from URL
And we need to create another function can help us load two images from URL
After that we can create a new function look like this, this function can notify when we get image from URL
Finally, inside the update button we put two URL into loadimage function as well as we need to say dispatchGroup.notify if the image is loaded sucessfully, this method is called
What’s Queues
Multithreading is mostly about “queues” in iOS.
Functions (usually closures) are simply lined up in a queue (like at the movies!).
Then those functions are pulled off the queue and executed on an associated thread(s). Queues can be “serial” (one closure a time) or “concurrent” (multiple threads servicing it)
Queues are following the FIFO pattern (First In, First Out), meaning that the queues first, it will be executed first and also finish first
Main Queues
There is a very special serial queue called the “main queue.”
All UI activity MUST occur on this queue and this queue only.
And, conversely, non-UI activity that is at all time consuming must NOT occur on that queue. We do this because we want our UI to be highly responsive!
And also because we want things that happen in the UI to happen predictably (serially). Functions are pulled off and worked on in the main queue only when it is “quiet”.
Global Queues
For non-main-queue work, you’re usually going to use a shared, global, concurrent queue.
List of DispatchQueue Priority
DispatchQoS.userInteractive : high priority, only do something short and quick DispatchQoS.userInitiated : high priority, but might take a little bit of time
DispatchQoS.background :not directly initiated by user, so can run as slow as needed
DispatchQoS.utility: long-running background processes, low priority
Serial
It’s executing one task at a time
What’s different Sync & Async
Sync when a work item is executed synchronously with sync method, the program waits until execution finishes before the method call returns
Async - execute asynchronously with the async method, the method call immediately
I have referenced document CS193p Stanford University
Conclusion
I hope you figure out when you read this article please leave your comment below this article if you have any advice for meI have referenced document CS193p Stanford University

Comments
Post a Comment