Search icon CANCEL
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 16 Programming for Beginners

You're reading from   iOS 16 Programming for Beginners Kickstart your iOS app development journey with a hands-on guide to Swift 5.7 and Xcode 14

Arrow left icon
Product type Paperback
Published in Nov 2022
Publisher Packt
ISBN-13 9781803237046
Length 686 pages
Edition 7th Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Ahmad Sahar Ahmad Sahar
Author Profile Icon Ahmad Sahar
Ahmad Sahar
Craig Clayton Craig Clayton
Author Profile Icon Craig Clayton
Craig Clayton
Arrow right icon
View More author details
Toc

Table of Contents (34) Chapters Close

Preface 1. Part I: Swift
2. Getting Familiar with Xcode FREE CHAPTER 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 II: Design
12. Setting Up the User Interface 13. Building Your User Interface 14. Finishing Up Your User Interface 15. Modifying and Configuring Cells 16. Part III: Code
17. Getting Started with MVC and Collection Views 18. Getting Data into Collection Views 19. Getting Started with Table Views 20. Getting Started with MapKit 21. Getting Started with JSON Files 22. Displaying Data in a Static Table View 23. Getting Started with Custom UIControls 24. Getting Started with Cameras and Photo Libraries 25. Understanding Core Data 26. Part IV: Features
27. Getting Started with Mac Catalyst 28. Getting Started with SwiftUI 29. Getting Started with Lock Screen Widgets 30. Getting Started with WeatherKit 31. Testing and Submitting Your App to the App Store 32. Other Books You May Enjoy
33. Index

Improving efficiency using async-let

Even though your app is now responsive to button taps and is able to update the user interface while the makeToast() and poachEgg() methods are running, both methods still execute sequentially. The solution here is to use async-let. Writing async in front of a let statement when you define a constant, and then writing await when you access the constant, allows parallel execution of asynchronous methods:

async let temporaryConstant1 = methodName1()
async let temporaryConstant2 = methodName2()
await variable1 = temporaryConstant1
await variable2 = temporaryConstant1

Here, methodName1() and methodName2() will run in parallel.

You will modify your app to use async-let to enable the makeToast() and poachEgg() methods to run in parallel. In the ViewController file, modify the code in the Task block as follows:

Task {
   let startTime = Date().timeIntervalSince1970
   toastLabel.text = "Making toast..."
   async let tempToast...
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