ebook img

Concurrency by Tutorials: Multithreading in Swift with GCD and Operations PDF

116 Pages·2019·12.761 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Concurrency by Tutorials: Multithreading in Swift with GCD and Operations

Concurrency by Tutorials Concurrency by Tutorials By Scott Grosch Copyright ©2019 Razeware LLC. Notice of Rights All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner. Notice of Liability This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software. Trademarks All trademarks and registered trademarks appearing in this book are the property of their own respective owners. raywenderlich.com 2 Concurrency by Tutorials Table of Contents: Overview About the Cover......................................................................................... 8 Acknowledgements............................................................................... 12 What You Need........................................................................................ 13 Book License............................................................................................. 14 Book Source Code & Forums............................................................. 15 Section I: Getting Started with Concurrency............ 17 Chapter 1: Introduction........................................................... 18 Chapter 2: GCD & Operations.............................................. 21 Section II: Grand Central Dispatch............................... 27 Chapter 3: Queues & Threads............................................... 28 Chapter 4: Groups & Semaphores....................................... 40 Chapter 5: Concurrency Problems...................................... 49 Section III: Operations...................................................... 57 Chapter 6: Operations.............................................................. 58 Chapter 7: Operation Queues............................................... 69 Chapter 8: Asynchronous Operations............................... 76 Chapter 9: Operation Dependencies.................................. 87 Chapter 10: Canceling Operations...................................... 97 Section IV: Real-Life Concurrency............................. 103 Chapter 11: Core Data.......................................................... 104 Chapter 12: Thread Sanitizer.............................................. 110 raywenderlich.com 3 Concurrency by Tutorials Conclusion.............................................................................................. 115 raywenderlich.com 4 Concurrency by Tutorials Table of Contents: Extended About the Cover. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 About the Editors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 About the Artist. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Acknowledgements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 What You Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Book License. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Book Source Code & Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Section I: Getting Started with Concurrency. . . . . . . 17 Chapter 1: Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 What is concurrency?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Why use concurrency?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 How to use concurrency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Chapter 2: GCD & Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Grand Central Dispatch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Which should you use?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Section II: Grand Central Dispatch . . . . . . . . . . . . . . . . . 27 Chapter 3: Queues & Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Dispatch queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Image loading example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 DispatchWorkItem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 raywenderlich.com 5 Concurrency by Tutorials Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Chapter 4: Groups & Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 DispatchGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Semaphores. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Chapter 5: Concurrency Problems . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Race conditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Deadlock. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Priority inversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Section III: Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Chapter 6: Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Reusability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Operation states. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 BlockOperation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Subclassing operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Chapter 7: Operation Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 OperationQueue management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Fix the previous project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Chapter 8: Asynchronous Operations . . . . . . . . . . . . . . . . . . . . . . . 76 Asynchronous operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Networked TiltShift. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Chapter 9: Operation Dependencies . . . . . . . . . . . . . . . . . . . . . . . . 87 Modular design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Specifying dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Watch out for deadlock. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Passing data between operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 raywenderlich.com 6 Concurrency by Tutorials Updating the table view controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Chapter 10: Canceling Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 97 The magic of cancel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Cancel and cancelAllOperations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Updating AsyncOperation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Canceling a running operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Section IV: Real-Life Concurrency. . . . . . . . . . . . . . . . 103 Chapter 11: Core Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 NSManagedObjectContext is not thread safe . . . . . . . . . . . . . . . . . . . . . . 105 Importing data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 NSAsynchronousFetchRequest. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Sharing an NSManagedObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Chapter 12: Thread Sanitizer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Why the sanitizer?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Getting started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Enabling sanitization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 It’s not code analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Xcode keeps getting smarter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Where to go from here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 raywenderlich.com 7 A About the Cover Our usual experience when looking at an animal is to see the creature and know, clearly, how its body is assigned and what each of its parts does — two legs for walking, two ears for hearing, a mouth for eating, gills for breathing, one brain for thinking. In looking at a starfish, however, things get a little trickier. Everything about the starfish is in multitudes that aren't always obvious to our eye: five–50 arms, a mouth with five jaws, a eyespot on each arm with 80–100 ocelli, a decentralized respiratory and central nervous system, a three-ringed circulatory system, a common “mouth” used both in consuming and excreting, and tubed “feet” that assist with sensing, moving and breathing. These marine invertebrates, including the Royal Starfish (Astropecten articulatus) that is found on the cover of this book, operate in the spirit of concurrency, having adapted so that the parts of their bodies have multiple functions — feet that help it move, feel, see and breathe, for example — for a simple but optimal life. Because of their adaptability and optimization of function, seastars are considered ecologically important as they, like the operations in this book, help their environment to be cleaner and more efficient. If you find yourself on the east coast of the continental Americas, especially the Caribbean, keep an eye out for a relatively small, impossibly purple seastar with brilliant orange edges. Learn more about the Royal Starfish, here: https://en.wikipedia.org/wiki/ Astropecten_articulatus. raywenderlich.com 8 Concurrency by Tutorials About the Cover Dedications "This book is dedicated to my wife and daughter, as well as to my parents who always made sure a good education was a priority." — Scott Grosch raywenderlich.com 9 Concurrency by Tutorials About the Cover About the Author Scott Grosch is the author of this book. He has been involved with iOS app development since the first release of the public SDK from Apple. He mostly works with a small set of clients on a couple large apps. During the day, Scott is a Solutions Architect at a Fortune 500 company in the Pacific Northwest. At night, he's still working on figuring out how to be a good parent to a toddler with his wife. About the Editors Marin Bencevic is the tech editor of this book. He is a Swift and Unity developer who likes to work on cool iOS apps and games, nerd out about programming, learn new things and then blog about it. Mostly, though, he just causes SourceKit crashes. He also has a chubby cat. Shai Mishali is the Final Pass Editor of this book. He's the iOS Tech Lead for Gett, the global on-demand mobility company; as well as an international speaker, and a highly active open-source contributor and maintainer on several high-profile projects - namely, the RxSwift Community and RxSwift projects. As an avid enthusiast of hackathons, Shai took 1st place at BattleHack Tel- Aviv 2014, BattleHack World Finals San Jose 2014, and Ford's Developer Challenge Tel-Aviv 2015. You can find him on GitHub and Twitter @freak4pc. Manda Frederick is the editor of this book. She has been involved in publishing for over 10 years through various creative, educational, medical and technical print and digital publications, and is thrilled to bring her experience to the raywenderlich.com family as Managing Editor. In her free time, you can find her at the climbing gym, backpacking in the backcountry, hanging with her dog, working on poems, playing guitar and exploring breweries. raywenderlich.com 10

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.