Coroutines

Path breaking and, probably, the most exciting feature in Kotlin are coroutines. They are a new way to write asynchronous, non-blocking code somewhere like the threads, but way more simple, efficient, and lightweight. Coroutines were added in Kotlin 1.1 and are still experimental, so think before using it in production.

In the later chapters of this book, you'll learn about Schedulers in RxKotlin, which encapsulates the complexities of threading, but you can use it only in RxKotlin chain, while you can use coroutines anywhere and everywhere. That is indeed a path-breaking feature of Kotlin. They provide a great abstraction on threads, making context changes and concurrency easier.

Keep in mind that RxKotlin does not use coroutines yet; the reason is quite simple–both coroutines and Schedulers in RxKotlin share nearly the same internal architecture; while coroutines are new, Schedulers have been there for a long time with RxJava, RxJs, RxSwift, and more.

Coroutines are the best fit for developers to implement concurrency when they're not using/can't use RxKotlin Schedulers.

So, let's start by adding it to our project. If you are using Gradle, follow these steps (apply plugin could be 'kotlin' or 'kotlin-android', depending on whether you use it for JVM or Android):

    apply plugin: 'kotlin' 
 
    kotlin { 
      experimental { 
        coroutines 'enable' 
      } 
    } 

And then, we have to add the following dependency:

    repositories { 
      ... 
      jcenter() 
    } 
    dependencies { 
      ... 
      compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.16" 
    } 

If you are using Maven, then add the following code block in the pom.xml file:

    <plugin> 
      <groupId>org.jetbrains.kotlin</groupId> 
      <artifactId>kotlin-maven-plugin</artifactId> 
      ... 
      <configuration> 
        <args> 
            <arg>-Xcoroutines=enable</arg> 
        </args> 
      </configuration> 
    </plugin> 
    <repositories> 
      ... 
      <repository> 
        <id>central</id> 
        <url>http://jcenter.bintray.com</url> 
      </repository> 
    </repositories> 
    <dependencies> 
      ... 
      <dependency> 
        <groupId>org.jetbrains.kotlin</groupId> 
        <artifactId>kotlinx-coroutines-core</artifactId> 
        <version>0.16</version> 
      </dependency> 
    </dependencies> 
Apache Maven is a software project management and comprehension tool. Based on the concept of a Project Object Model (POM), Maven can manage a project's build, reporting, and documentation from a central piece of information. Please refer to the following URL for more information–https://maven.apache.org/.

So, what exactly is a coroutine? While developing applications, we often come into situations where we need to perform long running or time taking operations, such as network call, database operations, or some complex computations. The only option in Java is to use a thread to handle such situations, which is very complex itself. Whenever we face those situations, we feel the need for a simple yet powerful API to handle such cases. Developers from the .NET domain, especially those who used C# before, are familiar with the async/await operators; this is somehow the closest to Kotlin coroutines.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.78.237