Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
iOS 18 Programming for Beginners

You're reading from   iOS 18 Programming for Beginners Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success

Arrow left icon
Product type Paperback
Published in Dec 2024
Publisher Packt
ISBN-13 9781836204893
Length 584 pages
Edition 9th Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (34) Chapters Close

Preface 1. Part 1: Swift FREE CHAPTER
2. Exploring Xcode 3. Simple Values and Types 4. Conditionals and Optionals 5. Range Operators and Loops 6. Collection Types 7. Functions and Closures 8. Classes, Structures, and Enumerations 9. Protocols, Extensions, and Error Handling 10. Swift Concurrency 11. Part 2: Design
12. Setting Up the User Interface 13. Building Your User Interface 14. Finishing Up Your User Interface 15. Modifying App Screens 16. Part 3: Code
17. Getting Started with MVC and Table Views 18. Getting Data into Table Views 19. Passing Data between View Controllers 20. Getting Started with Core Location and MapKit 21. Getting Started with JSON Files 22. Getting Started with Custom Views 23. Getting Started with the Camera and Photo Library 24. Getting Started with Search 25. Getting Started with Collection Views 26. Part 4: Features
27. Getting Started with SwiftData 28. Getting Started with SwiftUI 29. Getting Started with Swift Testing 30. Getting Started with Apple Intelligence 31. Testing and Submitting Your App to the App Store 32. Other Books You May Enjoy
33. Index

Understanding Swift concurrency

In Swift 5.5, Apple added support for writing asynchronous and parallel code in a structured way.

Asynchronous code allows your app to suspend and resume code. This allows your app to do things like update the user interface while still performing operations like downloading data from the internet.

Parallel code allows your app to run multiple pieces of code simultaneously.

You can find links to all of Apple's Swift concurrency videos during WWDC21 at https://developer.apple.com/news/?id=2o3euotz.

You can read Apple's Swift concurrency documentation at https://developer.apple.com/news/?id=2o3euotz.

During WWDC2024, Apple released Swift 6. With the Swift 6 language mode, the compiler can now guarantee that concurrent programs are free of data races. This means that code from one part of your app can no longer access the same area memory that is being modified by code from another part of your app. However, when you create a new Xcode project, it defaults to the Swift 5 language mode, and you have to turn on the Swift 6 language mode to enable this feature.

To view Apple’s WWDC24 video on migrating your app to Swift 6, click this link: https://developer.apple.com/videos/play/wwdc2024/10169/

To view Apple’s documentation on migrating your app to Swift 6, click this link: https://www.swift.org/migration/documentation/migrationguide/

To give you an idea of how Swift concurrency works, imagine that you are making soft-boiled eggs and toast for breakfast. Here is one way of doing it:

  1. Put two slices of bread into the toaster.
  2. Wait two minutes until the bread is toasted.
  3. Put two eggs in a pan containing boiling water, and cover them.
  4. Wait seven minutes until the eggs are cooked.
  5. Plate and serve your breakfast.

This takes nine minutes in total. Now think about this sequence of events. Do you spend that time just staring at the toaster and the pan? You'll probably be using your phone while the bread is in the toaster and the eggs are in the pan. In other words, you can do other things while the toast and eggs are being prepared. So, the sequence of events would be more accurately described as follows:

  1. Put two slices of bread into the toaster.
  2. Use your phone for two minutes until the bread is toasted.
  3. Put two eggs in a pan containing boiling water, and cover them.
  4. Use your phone for seven minutes until the eggs are cooked.
  5. Plate and serve your breakfast.

Here, you can see that your interaction with the toaster and pan can be suspended, then resumed, which means these operations are asynchronous. The operation still takes nine minutes, but you were able to do other things during that time.

There is another factor to consider. You don't need to wait for the bread to finish toasting before you put the eggs in the pan. This means you could modify the sequence of steps as follows:

  1. Put two slices of bread into the toaster.
  2. While the bread is toasting, put two eggs in a pan containing boiling water, and cover them.
  3. Use your phone for seven minutes until the eggs is cooked.
  4. Plate and serve your breakfast.

Toasting the bread and boiling the eggs are now carried out in parallel, which saves you two minutes. Great! Do note however that you have more things to keep track of.

Now that you understand the concepts of asynchronous and parallel operations, let's study the issues that an app without concurrency has in the next section.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime