you can do better with kotlin
play

You can do better with Kotlin Svetlana Isakova Kotlin Programming - PowerPoint PPT Presentation

You can do better with Kotlin Svetlana Isakova Kotlin Programming Language - modern - pragmatic - Android-friendly Official on Android Not only Android Pragmatic - tooling - Java interop From has good tooling - completion - navigation -


  1. await suspends computation fun processImage() = async { val deferred = loadImageAsync() val image = deferred.await() setImage(image) } await processImage processImage 2 1 loadImageAsync loadImageAsync

  2. await suspends computation …and continues it when result is ready processImage 2 loadImageAsync fun processImage() = async { val deferred = loadImageAsync() processImage val image = deferred.await() 3 setImage(image) } loadImageAsync

  3. await suspends computation …and continues it when result is ready processImage 2 loadImageAsync processImage 3 On which thread? loadImageAsync

  4. Q: On which thread can the coroutine be continued? A: You specify that.

  5. Continue in any thread from thread pool 1 async (CommonPool) { 
 ... 
 } 2 3 3

  6. Continue in the UI thread UI 1 async ( UI ) { ... UI 2 } UI 3

  7. Custom executor async (CustomContext) { 
 ... 
 } You can launch coroutine in the custom executor

  8. Two asynchronous computations fun overlay(first: Image, second: Image): Image fun overlayAsync() = async (CommonPool) { 
 val first = loadImageAsync ( "green" ) 
 val second = loadImageAsync ( "red" ) 
 overlay (first.await(), second.await()) 
 } overlayAsync 1 3 2 overlayAsync overlayAsync

  9. Programming with suspend functions

  10. Q: Can I define my custom suspend functions? A: Yes.

  11. Example: simple consecutive logic fun login(credentials: Credentials): UserID fun loadUserData(userID: UserID): UserData fun showData(data: UserData) fun showUserInfo(cred: Credentials) { val userID = login (credentials) val userData = loadUserData (userID) showData (userData) }

  12. Rewrite with async/await fun login(credentials: Credentials): Deferred<UserID> fun loadUserData(userID: UserID): Deferred<UserData> fun showData(data: UserData) fun showUserInfo(credentials: Credentials) = async (CommonPool) { val userID = login (credentials).await() val userData = loadUserData (userID).await() showData (userData) }

  13. Rewrite with suspend functions suspend fun login(credentials: Credentials): UserID suspend fun loadUserData(userID: UserID): UserData fun showData(data: UserData) suspend fun showUserInfo(credentials: Credentials) { val userID = login (credentials) val userData = loadUserData (userID) showData (userData) }

  14. RxJava / CompletableFuture vs Coroutines

  15. Rewrite with CompletableFuture fun loginAsync(credentials: Credentials): CompletableFuture<UserID> fun loadUserDataAsync(userID: UserID): CompletableFuture<UserData> fun showData(data: UserData) fun showUserInfo(credentials: Credentials) { loginAsync (credentials) .thenCompose { loadUserDataAsync ( it ) } .thenAccept { showData ( it ) } }

  16. Rewrite with RxJava fun login(credentials: Credentials): Single<UserID> fun loadUserData(userID: UserID): Single<UserData> fun showData(data: UserData) fun showUserInfo(credentials: Credentials) { login (credentials) .flatMap { loadUserData ( it ) } .doOnSuccess { showData ( it ) } .subscribe() }

  17. “Observables are great, but in many cases they’re kind of overkill.” somewhere on the Internet

  18. RxJava & coroutines Reactive Kotlin Streams coroutines

  19. kotlinx.coroutines async/await yield channels actors Library Language coroutines

  20. Experimental status of Coroutines • We want the community to try it! • Migration aids will be provided • Old code will continue to work via the support library

  21. kotlinx.coroutines • https://github.com/Kotlin/kotlinx.coroutines/ • Guide to kotlinx.coroutines by example • by Roman Elizarov (@relizarov)

Recommend


More recommend