Statistics
7
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-05-20
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Kotlin Coroutines by Tutorials By Filip Babić, Luka Kordić and Nishant Srivastava Copyright ©2022 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. Kotlin Coroutines by Tutorials Kotlin Coroutines by Tutorials raywenderlich.com 2
Table of Contents: Overview Book License 11............................................................................................. Before You Begin 12................................................................ What You Need 13........................................................................................ Book Source Code & Forums 14............................................................. Acknowledgments 18.................................................................................. Introduction 19.............................................................................................. Section I: Introduction to Coroutines 21.......................... Chapter 1: What Is Asynchronous Programming? 22........ Chapter 2: Setting Up Your Build Environments 40........... Chapter 3: Getting Started With Coroutines 52.................. Chapter 4: Suspending Functions 66........................................ Chapter 5: Async/Await 89........................................................... Chapter 6: Coroutine Context 112............................................ Chapter 7: Context Switch & Dispatching 119..................... Chapter 8: Exception Handling 130.......................................... Chapter 9: Manage Cancellation 144....................................... Section II: Channels & Flows 156........................................ Chapter 10: Building Sequences & Iterators With Yield 157............................................................................................... Chapter 11: Beginning With Coroutine Flow 171............... Chapter 12: SharedFlow & StateFlow 185............................. Kotlin Coroutines by Tutorials raywenderlich.com 3
Chapter 13: Testing Coroutines 199......................................... Section III: Coroutines & Android 210.............................. Chapter 14: Coroutines & Android 211................................... Chapter 15: Coroutines in the UI Layer 234.......................... Chapter 16: Networking With Coroutines 244.................... Chapter 17: Persistence & Coroutines 254........................... Chapter 18: Coroutines & Jetpack 267................................... Conclusion 287.............................................................................................. Kotlin Coroutines by Tutorials raywenderlich.com 4
Table of Contents: Extended Book License 11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Before You Begin 12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What You Need 13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book Source Code & Forums 14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Acknowledgments 18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Content Development 18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About Kotlin 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About Coroutines 20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to read this book 20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section I: Introduction to Coroutines 21. . . . . . . . . . . . . . Chapter 1: What Is Asynchronous Programming? 22. . . . . . . . . . . Providing Feedback 23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why Multithreading? 25. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interacting With the UI Thread From the Background 26. . . . . . . . . . . . . . . Handling Work Completion Using Callbacks 29. . . . . . . . . . . . . . . . . . . . . . . . . Indentation Hell 30. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Reactive Programming for Background Work 32. . . . . . . . . . . . . . . . . Diving Deeper Into the Complexity of Rx 33. . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Blast From the Past 34. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Explaining Coroutines: The Inner Work 35. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Variations Through History 36. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 38. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 39. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 2: Setting Up Your Build Environments 40. . . . . . . . . . . . . Choosing the Build Environments 41. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 5
Installing the IntelliJ IDEA 41. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing the Android Environment 45. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Importing a Project 48. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 51. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 51. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 3: Getting Started With Coroutines 52. . . . . . . . . . . . . . . . Executing Routines 53. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Launching a Coroutine 53. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building Coroutines 55. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Explaining Jobs 57. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Canceling Jobs 58. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Digging Deeper Into Coroutines 59. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Posting to the UI Thread 63. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 65. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 65. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 4: Suspending Functions 66. . . . . . . . . . . . . . . . . . . . . . . . . . . . Suspending vs. Non-Suspending 67. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Elaborating Continuations 79. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Your Own Suspendable API 83. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Returning Values Using withContext 85. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 87. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 88. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 5: Async/Await 89. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Async/Await Pattern 90. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Learning From the Past 90. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Async/Await 95. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deferring Values 99. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combining Multiple Deferred Values 100. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Being Cooperative and Structured 103. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 110. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 6
Where to Go From Here? 111. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 6: Coroutine Context 112. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Contextualizing Coroutines 113. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Providing Contexts 116. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 118. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 7: Context Switch & Dispatching 119. . . . . . . . . . . . . . . . . . Work Scheduling 120. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Coroutine Dispatcher Types 124. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Dispatchers 126. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 129. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 8: Exception Handling 130. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exception Propagation 131. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CoroutineExceptionHandler 133. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Try-Catch to the Rescue 135. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling Multiple Child Coroutine Exceptions 136. . . . . . . . . . . . . . . . . . . . . Callback Wrapping 138. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Supervising Coroutines 140. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 143. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 143. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 9: Manage Cancellation 144. . . . . . . . . . . . . . . . . . . . . . . . . . . Cancelling a Coroutine 145. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Timing Out 153. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 155. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 155. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section II: Channels & Flows 156. . . . . . . . . . . . . . . . . . . . . . Chapter 10: Building Sequences & Iterators With Yield 157. . . Getting Started With Sequences 158. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Enter: Sequence 161. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Generators & Sequences 163. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 7
Yielding From SequenceScope 165. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Providing Values With Yield & YieldAll 166. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 170. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 170. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 11: Beginning With Coroutine Flow 171. . . . . . . . . . . . . . Streams of Data 172. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Limitations of Streams 174. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A New Approach to Streams 175. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Flow Constraints 179. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 183. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 184. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 12: SharedFlow & StateFlow 185. . . . . . . . . . . . . . . . . . . . . . Getting Started 186. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sharing a Flow 186. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building a StateFlow 194. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 197. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 198. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 13: Testing Coroutines 199. . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting Started 200. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Writing Tests for Coroutines 201. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting Up the Test Environment 203. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summing it up 208. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 208. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section III: Coroutines & Android 210. . . . . . . . . . . . . . . . . Chapter 14: Coroutines & Android 211. . . . . . . . . . . . . . . . . . . . . . . . . The Importance of the Android Main Thread 211. . . . . . . . . . . . . . . . . . . . . . . Getting Started 212. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Doing Heavy Work on UI Thread 214. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Thread 216. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 8
Handler 218. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HandlerThread 220. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Executors 223. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . RxJava 226. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Coroutines 230. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 233. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 233. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 15: Coroutines in the UI Layer 234. . . . . . . . . . . . . . . . . . . . Getting Started 235. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing Lifecycle Scope 236. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Collecting Flows in the UI 237. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 243. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 243. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 16: Networking With Coroutines 244. . . . . . . . . . . . . . . . . Getting Started 245. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Network Call With Callbacks 245. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Coroutine-Powered Networking 247. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Retrofit Meets Coroutines 249. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 253. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 253. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 17: Persistence & Coroutines 254. . . . . . . . . . . . . . . . . . . . . Getting Started 255. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Suspending Database Calls 257. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Observing Database Changes 261. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Suspending Transactions 265. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 266. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 266. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 18: Coroutines & Jetpack 267. . . . . . . . . . . . . . . . . . . . . . . . . Coroutines in ViewModels 268. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comparing LiveData to Kotlin Flow 272. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 9
Coroutines & Flow in Jetpack Compose 277. . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing Coroutines on Android 283. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key Points 286. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go From Here? 286. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion 287. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Coroutines by Tutorials raywenderlich.com 10
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 11
Before You Begin This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more. raywenderlich.com 12
iWhat You Need To follow along with this book, you’ll need the following: • IntelliJ IDEA Community Edition 2022.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 Bumblebee: 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
iiBook Source Code & Forums Where to download the materials for this book The materials for this book can be cloned or downloaded from the GitHub book materials repository: • https://github.com/raywenderlich/kco-materials/tree/editions/3.0 Forums We’ve also set up an official forum for the book at https:// forums.raywenderlich.com/c/books/kotlin-coroutines. This is a great place to ask questions about the book or to submit any errors you may find. raywenderlich.com 14
“To my family and friends, but most of all to my fiancée. Thank you for all of your love and support, and for your patience during the process of writing this book.” — Luka Kordić “To my friends, family and everyone in the community who reached out and who I’ve helped, inspiring me in turn to help more people and share my knowledge.” — 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 raywenderlich.com 15
About the Authors Luka Kordić has been developing Android apps for 5 years. He enjoys sharing his knowledge by writing articles, holding lectures, and mentoring others. When not programming, he likes to play football, tennis, video games, and rock climbing. Filip Babić is an Android developer from Croatia and a Google Developer Expert for Android & Kotlin. He’s currently working on building awesome things with Jetpack Compose, teaching people about topics in Android and Kotlin and sharing his knowledge in various types of community engagement. Kotlin Coroutines by Tutorials About the Team raywenderlich.com 16
About the Editors Godfred Afful is a technical editor of this book. He is a software engineer specialized in backend and mobile applications development. He loves art, sports, nature and monkeys. Kevin Moore is a Tech Editor for this book. Kevin has been developing Android apps for over 10 years and at many companies. He’s written several articles, books and videos at raywenderlich.com. 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. John Hagemann is an editor of this book. He is a government program and policy analyst, technical writer, and editor, and has worked as a journalist and a writing instructor. 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 working at Whatsapp. He’s a musical theatre lover and a supporter of the soccer team S.P.A.L. Kotlin Coroutines by Tutorials About the Team raywenderlich.com 17
vAcknowledgments Content Development We would like to thank Nishant Srivastava for his contributions as an author on the previous editions of this book. raywenderlich.com 18
viIntroduction 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 its multiplatform support with JavaScript (ECMAScript 5.1) and native code (using LLVM). raywenderlich.com 19
About Coroutines 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 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 exceptions 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’ll learn everything about coroutines. If you need some specific knowledge about how to use them in the Android environment, the third section is 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. 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