How To Get Loads From Central Dispatch
We are going to take a look at threading and Grand Central Dispatch in Swift, we will explore what threads and queues are as well as the Grand Central Dispatch or GCD APIs is to manage your queues in the process, it will also touch on synchronous vs. asynchronous. Finish with an overview of the code and dispatch groups but this is something you want to learn in this tutorial.
What is Concurrency/Multithreading?:
We can optimize the performance of an app in
You will need to know concurrency to make your app more responsive and fast.
Concurrency is a condition in a program where more than one task is happening at the same time then it's called concurrency. iOS 4 supports multiple tasks to execute at the same time. W e will run multiple tasks at an equivalent time by adding some code. So you'll run tasks within the background (like downloading or processing data) while you retain your interface responsively.
In iOS, we can achieve concurrency in two ways.
- Using Grand Central Dispatch(GCD)
- Using Operations and OperationQueues
GCD (Grand Central Dispatch):
Grand Central Dispatch (GCD) is a low-level AP I for managing concurrent operations either asynchronously or synchronously. GCD The Grand Central Dispatch was first introduced in iOS 4, and it
GCD has lots of benefits like
- It improves application performance and responsiveness.
- The app will become more smooth.
- Execute multiple tasks at a time or one by one as per your requirements.
Thread:
Threading is an important concept in iOS. Thread in computer science is a way to run tasks simultaneously. Some languages support multithreading some not. The concept is pretty simple. This is what happens inside the processor. Consider launching an app in your iPhone. every iOS application has a main thread that is there to display your user interface and listen for events. complex computations may slow down the main thread and freeze the app and here's where multi-threading comes into play we must move all of the heavy liftings to a background thread and then move the result back onto the main thread. these threads might be executing their code simultaneously with a multi-core iOS device but they might also just be switching back and forth really quickly and some threads get a higher priority. Main Thread:
In iOS, there is a concept of the Main Thread or UI Thread. That thread becomes user interaction as a user button click.
The main thread is responsible for keeping the UI running smoothly and responding to user input. It can only execute one task at a time.
If
-Keep the UI thread free, to keep your app responsive
All iOS apps use the main thread to handle UI operations. Calling long-running operations from this main thread can lead Therefore, t ime-consuming processes should be done in a separate thread say a " Background thread " to free up the main UI thread so that the app remains responsive. If heavy operations like network calls, database access, and image downloading are performed on a background thread. Queues are data structures that manage objects An object that manages the execution of tasks serially or concurrently on your app's main thread or on a background thread. Dispatch queues are 1. Main Queue 2. Serial queues 3. Concurrent queues It is an object that manages the execution of tasks serially or concurrently on your app's main thread or on a background thread. (UIKit and AppKit) The main dispatch queue is a globally available serial queue executing tasks on the application's main thread. As the main thread is used for UI updates it's important to be conscious when executing tasks on this queue. Therefore, it's valuable to use the earlier described dispatch APIs to perform tasks on a different thread. You can start doing the heavy lifting on a background queue and dispatch back to the main queue when you're done. Serial queues execute one task at a time and Serial queues guarantee that Background Thread:
Queues:
Queuesare just a bunch of code blocks lined up or queued up waiting for a thread to execute them. you don't need to worry about threads in Swift only Queues the system takes care of providing and allocating threads to execute the code of the Queue. Dispatch Queues:
A dispatch queue is an object-like structure that manages the tasks you submit to it. All dispatch queues are first-in, first-out data structures. Thus, the tasks you add to a queue are always started in the same order that they were added.
Dispatch queues are thread-safe Different Types of Dispatch Queues:
Main Queue:
How about the main thread?:
let concurrentQueue = DispatchQueue(label: "appcodezip.concurrent.queue", attributes: .concurrent) concurrentQueue.async { // Perform the data request and JSON decoding on the background queue. fetchData() DispatchQueue.main.async { /// Access and reload the UI back on the main queue. tableView.reloadData() } }
Serial Queues:
Concurrent queues allow multiple tasks to run at Example with Serial Queue- As we will see from the examples above, a serial queue will complete each task within the order they're submitted to the queue. Each task will await the previous task to end before executing. As for the concurrent queue, each task doesn't serve the others within the queue and executes as soon as possible; the advantage is that each task on the queue will run at an equivalent time on separate threads, making a concurrent queue take less time than a serial queue. If the order of execution of tasks isn't important, always use a concurrent queue for the simplest efficiency. Concurrent queue often known as global dispatch queues can execute tasks simultaneously as a developer you can determine which of the backgroundQueues get more priority over other ones and you do this by specifying quality of service or QoS, we use Quality of Service class depending on how important a task is. Below are the Quality of Services. .userInteractive: user-interactive tasks have the highest priority of the system and use this class for tasks or queues that interact with the user or actively update your apps user interface for example use this class for animations or for tracking events interactively. .userInitiated: user-initiated tasks are second priority after the user-interactive. user-interactive tasks in their priority on the system you assign this class to tasks that provide immediate results for something the user is doing or that would prevent the user from using your app, for example, you might use this quality of service class to load the content of an email that you want to display to the user. The next two types are lower in priority for execution . .utility: Utility tasks have a lower priority than user initiated and user interactive tasks but a higher priority event background tasks you assign this quality of service class to tasks that do not permit the user from continuing to use your app. for example you might assign this class to long running tasks whose progress the user doesn't have to follow actively. .background: Background tasks have the lowest priority of all tasks you assign this class to tasks or dispatched queues that you use to perform work while your app is running in the background like maintenance tasks or cleanup. So the four priorities are listed here in the order of priority. . userInteractive . userInitiated . utility . background If you don't specify a QoS. .userInteractive .userInitiated .default .utility .background .default: It will be say assigned the default case The default task has a lower priority than user-interactive and user-initiated. Assign this class to the task that your app initiates or performs active work on the user's behalf. There are two ways that you can add code tears queue either synchronously or asynchronously. Synchronous: When a work item is executed synchronously with the sync method a program waits until the execution finishes before the method returns that is before the next line of code following the code block gets executed this means that it will block the user from doing anything until the code is completed the code block is executed as the closure on either the main or background queue like this. Asynchronous: When a work item is executed asynchronously the async method the method call returns immediately and the execution of the closure is done the same way as above we just use async instead of sync both synchronous and asynchronous executions can be done on the main or background threads but the fact of the matter is you almost never use asynchronous execution so we'll focus on asynchronous.
Concurrent queues:
Concurrent Dispatch Queue A concurrent dispatch queue runs as many tasks as it can without waiting for the started tasks to finish. Serial vs Concurrent Dispatch Queues with an example:
Program Execution- func serialQueues () { let serialQueue = DispatchQueue(label: "appcodezip.com") //default queue type is a serial queue let startDate = Date () for i in 0...4 { serialQueue.async { //run tasks on a background thread sleep(2) //do some task as webservice or database let timeTaken = Date().timeIntervalSince(startDate) print("serial Queue task \(i) done! total time taken: \(timeTaken)") } } }
Example with concurrent Queue -serial Queue task 0 done! total time taken: 2.0022469758987427 serial Queue task 1 done! total time taken: 4.005535006523132 serial Queue task 2 done! total time taken: 6.005913019180298 serial Queue task 3 done! total time taken: 8.007167935371399 serial Queue task 4 done! total time taken: 10.011394023895264
Program Execution-func concurrentQueues () { let concurrentQueue = DispatchQueue(label: "appcodezip.com", attributes: .concurrent) let startDate = Date () for i in 0...4 { concurrentQueue.async { sleep(2) //do some task as webservice or database let timeTaken = Date().timeIntervalSince(startDate) print("concurrent Queue task \(i) done! total time taken: \(timeTaken)") } } }
concurrent Queue task 2 done! total time taken: 2.001850962638855 concurrent Queue task 3 done! total time taken: 2.0018709897994995 concurrent Queue task 1 done! total time taken: 2.0017420053482056 concurrent Queue task 4 done! total time taken: 2.0018869638442993 concurrent Queue task 0 done! total time taken: 2.0017420053482056
// User-interactive DispatchQueue.global(qos: .userInteractive).async { // Event handling task & Animation task // Task compete then update UI (in main queue) } // User-initiated: DispatchQueue.global(qos: .userInitiated).async { // Load conent of email, etc } // Defaul DispatchQueue.global(qos: .default).async { // task that your app initiates // performs active work on the user's behalf } // Utility DispatchQueue.global(qos: .utility).async { //Low priority task } // Background DispatchQueue.global(qos: .background).async { // task which needs to be executed when the app is running in the background }
DispatchQueue.main.sync{ //code to be execute }
DispatchQueue.global().async{ //code to be execute }
If you enjoyed this article please share.
Thanks for reading!!
Similar solutions you may also like...
-
How many ways to achieve Concurrency/Multithreading iOS getting started with Grand Central Dispatch (GCD) Swift 5.2 ?
-
Choosing Between NSOperation and Grand Central Dispatch in iOS Swift 5 | Using Operations and OperationQueues in iOS Swift 5
-
When should we choose Any or AnyObject?
-
Memory Management with ARC and Avoiding retain cycles with Weak and Unowned in Swift.
-
Implement Your Own Side Menu (Hamburger Menu) Swift 5.2
-
How to keep Alerts with UIAlertController in Swift 5.2
-
Lazy Stored Properties in swift! How to work static and final in iOS | Static Property | Class Property | Final Property in iOS Swift
-
How to parse a JSON file using JsonDecoder in swift 5.2?
-
App called -statusBar or -status bar window on UIApplication. Use the status bar manager object on the window scene instead.
-
Swift 5.2 Generics Explained - How to Use Generics in iOS Apps
-
How to generate .ipa file for In-House and distribute enterprise iOS app inside a company
How To Get Loads From Central Dispatch
Source: https://www.appcodezip.com/2020/10/Concurrency-GCD.html
Posted by: hopsonticheir.blogspot.com
0 Response to "How To Get Loads From Central Dispatch"
Post a Comment