(This page has no text content)
Kotlin Corou+nes by Tutorials By Filip Babić and Nishant Srivastava Copyright ©2019 Razeware LLC. No+ce 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. No+ce 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. Kotlin Coroutines by Tutorials Kotlin Coroutines by Tutorials raywenderlich.com 2
Dedica+ons "To my friends and family. And mostly to my loved one. Thank you for being patient and understanding, when I couldn’t grab a cup of coffee or tea and catch up. Huge thanks to everyone who’s supported me throughout the entire process, with positive and motivational encouragement. This wouldn’t have gone as nearly as smooth without you." — Filip Babić "I would like to thank the many people who have made this book possible. To my father, who gave me the desire to be a curious soul and learn more. To my mom, who has supported me all along whenever I have had doubts about my own capabilities as a writer. To my friends, Saachi Chawla and Kirti Dohrey, who have always believed in me during my ups and downs. To people who have directly or indirectly been my mentor and helped me through understanding technology at a deeper level whenever I found myself stuck. And lastly, to the team at raywenderlich.com, my co-author, editors and everyone involved in making this book a reality." — Nishant Srivastava Kotlin Coroutines by Tutorials Kotlin Coroutines by Tutorials raywenderlich.com 3
About the Authors Nishant Srivastava is an author on this book. Nishant is a Sr.Android Engineer at Soundbrenner in Berlin, Germany and an open source enthusiast who spends his time doodling when not hacking on Android. He is a caffeine-dependent life-form and can be found either talking about android libraries or advocating that coffee is the elixir of life at community gatherings. He has been part of two startups in the past (Founding Team Member at OmniLabs, Inc. and one of the first employees at Silverpush) with experience in Android SDK Engineering and Audio Digital Signal Processing(DSP) on Android. While working at his past company (Silverpush), he developed the company’s patented UAB (Unique Audio Beacon) Technology. Filip Babić is an author of this book. He is an experienced Android developer from Croatia, working at the Five Agency, building world-known applications, such as the RosettaStone language- learning application and AccuWeather, the globally known weather reporting app. Previously he worked at COBE d.o.o., a German- owned mobile agency, which is partners with the biggest German media company. He's enthusiastic about the Android ecosystem, focusing extensively on applying Kotlin to Android applications, and building scalable, testable and user-friendly applications. Passionately building up good spirit in local development groups in Croatia, focusing on lectures, education, and engagement of new, aspiring developers in the Croatian IT community. But also pursuing global conferences, meetups, and IT fests. Altruistic when it comes to consulting and mentoring, trying to give help to everyone, whenever possible, motivated by the ideology that the Android ecosystem we live in is only as good as we make it. Kotlin Coroutines by Tutorials Kotlin Coroutines by Tutorials raywenderlich.com 4
About the Editors Eric Crawford is a technical editor of this book. Eric is a Senior Software Developer at John Deere, where he bounces between iOS and Android development. Before coming to Deere he did freelance mobile development and serverside web development utilizing Java. In his free time he likes to dabble into other platforms like IOT and cloud computing. Kevin Moore is a technical editor for the book. He has been developing Android apps for over 9 years and at many companies. He's written several articles at www.raywenderlich.com and created the "Programming in Kotlin" video series. He enjoys creating apps for fun and teaching others how to write Android apps.In addition to programming, he loves playing Volleyball and running the sound system at church. Massimo Carli is the final pass editor of this book. Massimo has been working with Java since 1995 when he co-founded the first Italian magazine about this technology (http://www.mokabyte.it). After many years creating Java desktop and enterprise application, he started to work in the mobile world. In 2001 he wrote his first book about J2ME. After many J2ME and Blackberry applications, he then started to work with Android in 2008. The same year he wrote the first Italian book about Android; best seller on Amazon.it. That was the first of a series of 8 books. he worked at Yahoo and Facebook and he's actually Engineering Tech Lead at Lloyds. He's a musical theatre lover and a supporter of the soccer team S.P.A.L. About the Ar+st Vicki Wenderlich is the designer and artist of the cover of this book. She is Ray’s wife and business partner. She is a digital artist who creates illustrations, game art and a lot of other art or design work for the tutorials and books on raywenderlich.com. When she’s not making art, she loves hiking, a good glass of wine and attempting to create the perfect cheese plate. Kotlin Coroutines by Tutorials Kotlin Coroutines by Tutorials raywenderlich.com 5
Table of Contents: Overview What You Need 13.............................................................................. Book License 14................................................................................... Book Source Code & Forums 15...................................................... Book Updates 17.................................................................................. About the Cover 18............................................................................. IntroducLon 19..................................................................................... Sec+on I: Introduc+on to Corou+nes 21......................... Chapter 1: What Is Asynchronous Programming? 23....... Chapter 2: SeSng Up Your Build Environments 39.......... Chapter 3: GeSng Started with CorouLnes 52................. Chapter 4: Suspending FuncLons 65.................................... Chapter 5: Async/Await 83..................................................... Chapter 6: CorouLne Context 104....................................... Chapter 7: CorouLne Contexts & Dispatchers 110.......... Chapter 8: ExcepLon Handling 120...................................... Chapter 9: Manage CancellaLon 132................................... Sec+on II: Channels & Flows 143...................................... Chapter 10: Building Sequences & Iterators with Yield 144..................................................................................... Chapter 11: Channels 159...................................................... Chapter 12: Broadcast Channels 190................................... Chapter 13: Producer & Actors 205...................................... Kotlin Coroutines by Tutorials raywenderlich.com 6
Chapter 14: Beginning with CorouLnes Flow 222............ Chapter 15: TesLng CorouLnes 235..................................... Sec+on III: Corou+nes & Android 246............................. Chapter 16: Android Concurrency Before CorouLnes 247.......................................................................... Chapter 17: CorouLnes on Android - Part 1 286.............. Chapter 18: CorouLnes on Android - Part 2 309.............. Conclusion 332..................................................................................... Kotlin Coroutines by Tutorials raywenderlich.com 7
Table of Contents: Extended What You Need 13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book License 14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book Source Code & Forums 15. . . . . . . . . . . . . . . . . . . . . . . . . . Book Updates 17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Cover 18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . IntroducLon 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About Kotlin 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About CorouLnes 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to read this book 20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sec+on I: Introduc+on to Corou+nes 21. . . . . . . . . . . . Chapter 1: What Is Asynchronous Programming? 23. . . . . . . . . Providing feedback 23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why mulLthreading? 25. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . InteracLng with the UI thread from the background 26. . . . . . . . . . . . . . Handling work compleLon using callbacks 30. . . . . . . . . . . . . . . . . . . . . . IndentaLon hell 31. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using reacLve extensions for background work 32. . . . . . . . . . . . . . . . . . Diving deeper into the complexity of Rx 34. . . . . . . . . . . . . . . . . . . . . . . . A blast from the past 35. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Explaining corouLnes: The inner works 35. . . . . . . . . . . . . . . . . . . . . . . . . VariaLons through history 36. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 37. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 38. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 2: SeSng Up Your Build Environments 39. . . . . . . . . . Choosing the build environments 39. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing the IntelliJ IDEA 40. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 8
Building the Android environment 45. . . . . . . . . . . . . . . . . . . . . . . . . . . . . ImporLng a project 47. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 51. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 51. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 3: GeSng Started with CorouLnes 52. . . . . . . . . . . . . . ExecuLng rouLnes 52. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Launching a corouLne 53. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building corouLnes 54. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Explaining jobs 56. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Canceling Jobs 57. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Digging deeper into corouLnes 58. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . PosLng to the UI thread 62. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 63. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 64. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 4: Suspending FuncLons 65. . . . . . . . . . . . . . . . . . . . . . . Suspending vs. non-suspending 65. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ElaboraLng conLnuaLons 76. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CreaLng your own suspendable API 80. . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 82. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 82. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 5: Async/Await 83. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The async/await padern 83. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Learning from the past 84. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using async/await 88. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deferring values 92. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combining mulLple deferred values 93. . . . . . . . . . . . . . . . . . . . . . . . . . . . Being cooperaLve and structured 96. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 101. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 103. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 6: CorouLne Context 104. . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 9
Contextualizing corouLnes 104. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Providing contexts 107. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 109. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 7: CorouLne Contexts & Dispatchers 110. . . . . . . . . . Work scheduling 110. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CorouLne dispatcher types 114. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using dispatchers 116. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 119. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 8: ExcepLon Handling 120. . . . . . . . . . . . . . . . . . . . . . . . ExcepLon propagaLon 120. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling excepLons 121. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Callback wrapping 128. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 131. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 131. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 9: Manage CancellaLon 132. . . . . . . . . . . . . . . . . . . . . . Cancelling a corouLne 132. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 141. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 142. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sec+on II: Channels & Flows 143. . . . . . . . . . . . . . . . . . . Chapter 10: Building Sequences & Iterators with Yield 144. . . GeSng started with sequences 144. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Enter: Sequence 147. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Generators and Sequences 150. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . SequenceScope is here to stay 152. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Yield and YieldAll at your service 153. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 157. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 158. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 11: Channels 159. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . GeSng started with channels 160. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 10
Pipelines 167. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Buffered channel 178. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comparing send and offer 180. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comparing receive and poll 182. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Error handling 183. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comparing Channels to Java Queues 186. . . . . . . . . . . . . . . . . . . . . . . . . . Key points 189. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 12: Broadcast Channels 190. . . . . . . . . . . . . . . . . . . . . . GeSng started with broadcast channels 191. . . . . . . . . . . . . . . . . . . . . . . ConflatedBroadcast channel 196. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ReacLveX vs. BroadcastChannel 199. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 204. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 204. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 13: Producer & Actors 205. . . . . . . . . . . . . . . . . . . . . . . Producing and consuming data 205. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Producer-consumer problem 206. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . AcLng upon data 212. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 220. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 221. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 14: Beginning with CorouLnes Flow 222. . . . . . . . . . . Streams of data 222. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . LimitaLons of streams 225. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A new approach to streams 226. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Flow Constraints 229. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 232. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 15: TesLng CorouLnes 235. . . . . . . . . . . . . . . . . . . . . . . GeSng started 236. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . WriLng tests for CorouLnes 237. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . SeSng up the test environment 239. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summing it up 244. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 11
Key points 244. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sec+on III: Corou+nes & Android 246. . . . . . . . . . . . . . . Chapter 16: Android Concurrency Before CorouLnes 247. . . . GeSng started 248. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Does Android need corouLnes? 250. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CorouLnes 280. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing Anko 282. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 283. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 285. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 17: CorouLnes on Android - Part 1 286. . . . . . . . . . . . GeSng started 287. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What’s in the context? 291. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ConverLng exisLng API call to use corouLnes 298. . . . . . . . . . . . . . . . . . CorouLnes and Android lifecycle 301. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 308. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 308. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 18: CorouLnes on Android - Part 2 309. . . . . . . . . . . . GeSng started 310. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Debugging corouLnes 312. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ExcepLon handling 315. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Don’t forget tesLng 320. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Anko: Simplified corouLnes 327. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 330. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion 332. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 12
WWhat You Need To follow along with this book, you'll need the following: • IntelliJ IDEA Community Edition 2019.1.x: Available at https:// www.jetbrains.com/idea/. This is the environment in which you'll develop most of the sample code in this book. • Jave SE Development Kit 8.: Most of the code in this book will be run on the Java Virtual Machine or JVM, for which you need a Java Development Kit or JDK. The JDK can be downloaded from Oracle at http://www.oracle.com/technetwork/java/ javase/downloads/index.html. • Android Studio 3.x.: For the examples about Android described in Section 3, you can use the IDE available at https://developer.android.com/studio/. If you haven't installed the latest versions of IntelliJ IDEA Community Edition and JDK 8, be sure to do that before continuing with the book. Chapter 2: "Setting Up Your Build Environments" will show you how to get started with IntelliJ IDEA to run Kotlin coroutines code on the JVM. raywenderlich.com 13
LBook License By purchasing Kotlin Coroutines by Tutorials, you have the following license: • You are allowed to use and/or modify the source code in Kotlin Coroutines by Tutorials in as many apps as you want, with no attribution required. • You are allowed to use and/or modify all art, images and designs that are included in Kotlin Coroutines by Tutorials in as many apps as you want, but must include this attribution line somewhere inside your app: “Artwork/images/designs: from Kotlin Coroutines by Tutorials, available at www.raywenderlich.com.” • The source code included in Kotlin Coroutines by Tutorials is for your personal use only. You are NOT allowed to distribute or sell the source code in Kotlin Coroutines by Tutorials without prior authorization. • This book is for your personal use only. You are NOT allowed to sell this book without prior authorization, or distribute it to friends, coworkers or students; they would need to purchase their own copies. All materials provided with this book are provided on an “as is” basis, without warranty of any kind, express or 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 an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. All trademarks and registered trademarks appearing in this guide are the properties of their respective owners. raywenderlich.com 14
BBook Source Code & Forums If you bought the digital edi+on The digital edition of this book comes with the source code for the starter and completed projects for each chapter. These resources are included with the digital edition you downloaded from store.raywenderlich.com. If you bought the print version You can get the source code for the print edition of the book here: https://store.raywenderlich.com/products/kotlin-coroutines-by-tutorials-source- code Forums We’ve also set up an official forum for the book at forums.raywenderlich.com. This is a great place to ask questions about the book or to submit any errors you may find. Digital book edi+ons We have a digital edition of this book available in both ePUB and PDF, which can be handy if you want a soft copy to take with you, or you want to quickly search for a specific term within the book. Buying the digital edition version of the book also has a few extra benefits: free updates each time we update the book, access to older versions of the book, and you can download the digital editions from anywhere, at anytime. raywenderlich.com 15
Visit our Kotlin Coroutines by Tutorials store page here: • https://store.raywenderlich.com/products/kotlin-coroutines-by-tutorials. And if you purchased the print version of this book, you’re eligible to upgrade to the digital editions at a significant discount! Simply email support@razeware.com with your receipt for the physical copy and we’ll get you set up with the discounted digital edition version of the book. Kotlin Coroutines by Tutorials Book Source Code & Forums raywenderlich.com 16
BBook Updates Since you’ve purchased the digital edition version of this book, you get free access to any updates we may make to the book! The best way to get update notifications is to sign up for our monthly newsletter. This includes a list of the tutorials that came out on raywenderlich.com that month, any important news like book updates or new books, and a list of our favorite iOS development links for that month. You can sign up here: • www.raywenderlich.com/newsletter raywenderlich.com 17
AAbout the Cover It would be difficult to think of an animal that cooperates more effectively and asynchronously with others of its kind than a bee. Beyond that, bees famously work with their environment to keep entire natural ecosystems and man-made industries thriving. Their community and industry has been well documented and celebrated throughout time, with references by philosopher Aristotle, economic theorist Karl Marx and even comedian Jerry Seinfeld. Like the coroutines explored in this book, the various 16,000 known species of bees live harmoniously in their intricate colonies — an elegant network of productivity and execution of duties. Developers can easily take great insight from the bee, working to architect code in an asynchronous way in which each part plays a critical role to contribute to the overall success of the project at hand. Learn more about these important animals, here: https://en.wikipedia.org/wiki/Bee. raywenderlich.com 18
IIntroduc+on Coroutines with Kotlin represents one of the most interesting and fasinating challenges in the software engineering world. It’s an opportunity to implement complex concurrent tasks in an elegant and performant way. Reading this book will give you the opportunity to learn the basic concepts about multithreading and how concurrent programming can be simplified using Kotlin and Coroutines. About Kotlin Kotlin is a general purpose, open source, statically typed “pragmatic” programming language for the JVM that combines object-oriented and functional programming features. It originated at JetBrains, the company that drives IntelliJ IDEA, and has been open source since 2012. At Google I/O 2017 Kotin was officially supported by Google as the main language for developing Android applications. It is a language focused on interoperability, safety, clarity, and tooling support. It is also important to mention it’s multiplatform support with JavaScript (ECMAScript 5.1) and native code (using LLVM). About Corou+nes Asynchronous programming is often tedious and error-prone. The extensive usage of callbacks makes the code hard to read, debug and test. Coroutines define a different paradigm which introduces the concept of suspending functions. Coroutines raywenderlich.com 19
generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed. In this way, you can write asynchronous code as if it were synchronous. Coroutines are a recent solution in Kotlin and Android environment for writing concurrent and asynchronous code. How to read this book This book contains four sections. The first section is an introduction to multithreading and concurrent programming with Kotlin. It explains how you can execute asynchronous tasks using the Kotlin language, what problems you may face and how coroutines can be a valid solution. You'll be introduced to the fundamental concepts of suspending functions, coroutine context and dispatching. You'll also learn how to manage exception and how to handle errors with coroutines. The second section explains, in detail and with several examples, how to use very important API based on coroutine technology. You'll learn how to create sequences and iterators. Using channels, you'll learn how different coroutines can communicate and exchange data in thread safe way. Finally you'll learn all the details about coroutine flow which are a fundamental part of the last version. In the first two sections, you learned everything about coroutines. If you need some specific knowledge about how to use them in the Android environment, this is the section for you. You’ll create a complete Android application and you’ll see how to use coroutines in order to create a very responsive application. Kotlin can be used on JVM but also in other enviroments; this is called multiplatform. In this forth section you'll learn how to use coroutine with other languages like Java. The best way to learn about Kotlin Coroutines is to roll up your sleeves and start coding. Enjoy the book! Kotlin Coroutines by Tutorials Introduction raywenderlich.com 20
Comments 0
Loading comments...
Reply to Comment
Edit Comment