Kotlin Coroutines by Tutorials by By Filip Babić & By Nishant Srivastava
Author:By Filip Babić & By Nishant Srivastava
Language: eng
Format: epub
Publisher: Ray Wenderlich
Create a channel with default — i.e., 0 capacity.
Set up the producer.
Send data in the channel.
Conditional check, if the current item is equal to value Pear.
Signal the closure of the channel via calling close() on the channel.
Set up the consumer that is printing the received values using for loop (until the channel is closed).
Print the final Done status.
In the previous example, you create a Channel of String objects. Then, into the body of the launch coroutine builder, you iterate over an Array<String> and put each element into the channel using the send function. While iterating, you check if the current value equals to Pear, in which case you close the channel invoking the close method. This is an example of a condition for the closing of the channel.
On the receiving side, you use a normal iteration with a for cycle in order to consume all the elements available in the channel. The for cycle is smart enough to understand when the channel is closed because it uses the underlying Iterator.
The for loop solution is excellent because it allows you to use channels in the normal pattern that you’d use for iterating over a normal collection. If you want more control over what you’re doing, you can consume the channel using code like this:
while (!kotlinChannel.isClosedForReceive) { val value = kotlinChannel.receive() println(value) }
However, there is yet another way to iterate over the channel values, via using repeat() Kotlin construct:
// Another way to iterate over the channel values // You use channel.receive() to // get the messages one by one repeat(3){ val fruit = kotlinChannel.receive() println(fruit) }
Here, you explicitly use the receive method but you have to know exactly how many elements you’re getting from the channel, which is not always possible. If you try to put 4 instead of 3 as argument of the repeat function, you’ll have a ClosedReceiveChannelException exception like this:
Apple Banana Pear Exception in thread "main" kotlinx.coroutines.channels.ClosedReceiveChannelException: Channel was closed at kotlinx.coroutines.channels.Closed.getReceiveException(AbstractChannel.kt:1070)
It’s interesting to note that the exception is not thrown on the receive function but on the close one. This happens because the close function is a suspend function, which actually completes only when the receiver consumes all the items in the channel. If the receiver requests more items that the one available, the producer tries to provide some new data. But, in this, case the channel is closed and this is not possible. This is the reason for the ClosedReceiveChannelException. In the case that you put a value smaller than the number of available objects, on the other hand, you’re going to miss some data.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Access | Data Mining |
Data Modeling & Design | Data Processing |
Data Warehousing | MySQL |
Oracle | Other Databases |
Relational Databases | SQL |
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Azure Data and AI Architect Handbook by Olivier Mertens & Breght Van Baelen(6714)
Building Statistical Models in Python by Huy Hoang Nguyen & Paul N Adams & Stuart J Miller(6692)
Serverless Machine Learning with Amazon Redshift ML by Debu Panda & Phil Bates & Bhanu Pittampally & Sumeet Joshi(6563)
Data Wrangling on AWS by Navnit Shukla | Sankar M | Sam Palani(6347)
Driving Data Quality with Data Contracts by Andrew Jones(6297)
Machine Learning Model Serving Patterns and Best Practices by Md Johirul Islam(6067)
Learning SQL by Alan Beaulieu(5994)
Weapons of Math Destruction by Cathy O'Neil(5778)
Big Data Analysis with Python by Ivan Marin(5352)
Data Engineering with dbt by Roberto Zagni(4349)
Solidity Programming Essentials by Ritesh Modi(3996)
Time Series Analysis with Python Cookbook by Tarek A. Atwan(3854)
Pandas Cookbook by Theodore Petrou(3565)
Blockchain Basics by Daniel Drescher(3292)
Hands-On Machine Learning for Algorithmic Trading by Stefan Jansen(2905)
Feature Store for Machine Learning by Jayanth Kumar M J(2811)
Learn T-SQL Querying by Pam Lahoud & Pedro Lopes(2794)
Mastering Python for Finance by Unknown(2743)
