ANDROID_LIVE Telegram 934
​​Select в Kotlin Coroutines
#kotlin

Давненько на канале не появлялось сообщений. 🙄 Но я снова с вами, и сегодня предлагаю обсудить ключевое слово select, но только не в контексте SQL-запросов, а в контексте Coroutines.

В мобильной разработке его можно использовать для кейсов, когда у нас есть несколько suspend-функций, но нас интересует результат только той, которая завершится первой.

Небольшой пример:
val winner = select<String> {
data1().onAwait { it }
data2().onAwait { it }
}

println("The winner = $winner") // prints "The winner = Hello"


Функции data1 и data2 — это простейшие вызовы с delay:
fun data1() = GlobalScope.async {
delay(1000)
"Hello"
}

fun data2() = GlobalScope.async {
delay(2000)
"World"
}


В общем, ещё один суперполезный инструмент при использовании Coroutines. Подробнее можно почитать тут.
🔥13🍾8👍5👌1🏆1



tgoop.com/android_live/934
Create:
Last Update:

​​Select в Kotlin Coroutines
#kotlin

Давненько на канале не появлялось сообщений. 🙄 Но я снова с вами, и сегодня предлагаю обсудить ключевое слово select, но только не в контексте SQL-запросов, а в контексте Coroutines.

В мобильной разработке его можно использовать для кейсов, когда у нас есть несколько suspend-функций, но нас интересует результат только той, которая завершится первой.

Небольшой пример:
val winner = select<String> {
data1().onAwait { it }
data2().onAwait { it }
}

println("The winner = $winner") // prints "The winner = Hello"


Функции data1 и data2 — это простейшие вызовы с delay:
fun data1() = GlobalScope.async {
delay(1000)
"Hello"
}

fun data2() = GlobalScope.async {
delay(2000)
"World"
}


В общем, ещё один суперполезный инструмент при использовании Coroutines. Подробнее можно почитать тут.

BY Android Live 🤖




Share with your friend now:
tgoop.com/android_live/934

View MORE
Open in Telegram


Telegram News

Date: |

Telegram iOS app: In the “Chats” tab, click the new message icon in the right upper corner. Select “New Channel.” The visual aspect of channels is very critical. In fact, design is the first thing that a potential subscriber pays attention to, even though unconsciously. On Tuesday, some local media outlets included Sing Tao Daily cited sources as saying the Hong Kong government was considering restricting access to Telegram. Privacy Commissioner for Personal Data Ada Chung told to the Legislative Council on Monday that government officials, police and lawmakers remain the targets of “doxxing” despite a privacy law amendment last year that criminalised the malicious disclosure of personal information. Over 33,000 people sent out over 1,000 doxxing messages in the group. Although the administrators tried to delete all of the messages, the posting speed was far too much for them to keep up. The court said the defendant had also incited people to commit public nuisance, with messages calling on them to take part in rallies and demonstrations including at Hong Kong International Airport, to block roads and to paralyse the public transportation system. Various forms of protest promoted on the messaging platform included general strikes, lunchtime protests and silent sit-ins.
from us


Telegram Android Live 🤖
FROM American