4/16/2023 0 Comments Swift share array between threadsLet processedImage = upscaleAndFilter(image: image) Then we update the UI with the results on the main queue: (with: url) When performing a long-running task (network call, computationally intensive work, etc), we avoid freezing the UI by performing this work on a background queue. This queue is responsible for drawing your application’s UI and responding to user interactions (touch, scroll, pan, etc.) If you block this queue for too long, your iOS app will appear to freeze, and your macOS app will display the infamous beach ball/spinning wheel. Private queues (can be serial or concurrent, you create them)Įvery app comes with a Main queue, which is a serial queue that executes tasks on the main thread.Global queues (concurrent, pre-defined).The Main dispatch queue (serial, pre-defined).Note that I said started, because the completion time of your tasks depends on several factors, and is not guaranteed to be FIFO (more on that later.)īroadly speaking, there are three kinds of queues available to you: The tasks in dispatch queues are always started in a first-in, first-out (FIFO) fashion. These are the main building blocks of GCD which let you execute arbitrary blocks of code using a set of parameters that you define. GCD manages a thread pool for you, and it will scale from a single-core Apple Watch all the way up to a many-core MacBook Pro. GCD will handle creating the requisite threads and will schedule your tasks on those threads, shifting the burden of thread management from the developer to the system.Ī big advantage of GCD is that you don’t have to worry about hardware resources as you write your concurrent code. Instead of creating threads directly, you use GCD to schedule work tasks, and the system will perform these tasks for you by making the best use of its resources. With GCD, Apple took an asynchronous design approach to the problem. Grand Central Dispatch was created by Apple over 10 years ago as an abstraction to help developers write multi-threaded code without manually creating and managing the threads themselves. However, threads are a low-level tool, and managing threads manually in an efficient manner is extremely difficult. To make use of these cores, you will need to use multiple threads. Concurrency and Grand Central DispatchĬoncurrency lets you take advantage of the fact that your device has multiple CPU cores. Feel free to skip to the Sync vs Async section if you are already familiar with the topic. Let’s start with a brief intro to GCD and dispatch queues. Finally, I will end with a summary and some general advice. Afterwards, I will cover some pitfalls when working with concurrency. I will begin with an introduction to GCD, followed by a detailed explanation on sync, async, serial and concurrent. Download the source code here, or get the public beta here. The app also has a fun short quiz that I encourage you to try before and after reading this article. I created a SwiftUI companion app to visually demonstrate the concepts in this article. And I hope you will pick up one or two new things along the way. If you have some experience with GCD, but are still curious about the topics mentioned above, I think you will still find it useful. If you've never used GCD before, this article is a great place to start. In particular, I wish to explore the differences between serial and concurrent queues, as well as the differences between synchronous and asynchronous execution. So in this article I want to zoom in on a sub-topic concerning queues and the Grand Central Dispatch (GCD) framework.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |