Jonathan Walsh
08/06/2019, 4:24 PMsuspendCoroutine
within a withTimeout
call, but the timeout doesn't seem to work. Has anyone run into similar issues?Aldo Wachyudi
08/06/2019, 5:24 PMDerek Berner
08/06/2019, 7:25 PMimport kotlinx.coroutines.*
@JvmName("_memoize_receiver")
fun <T,R> (suspend (T) -> R).memoize(): suspend (T) -> R {
val memos = mutableMapOf<T, Deferred<R>>()
val f = this
return { t ->
coroutineScope {
withContext(coroutineContext + <http://Dispatchers.IO|Dispatchers.IO>) {
synchronized(memos) {
memos.computeIfAbsent(t) { async { f(t) } }
}
}.await()
}
}
}
fun <T,R> memoize(f: suspend (T) -> R): suspend (T) -> R = f.memoize()
Pablichjenkov
08/06/2019, 7:59 PMsuspend fun doSomethingAndThrow() { throw(MyException()) }
val coroutineScope = CoroutineScope(Job())
coroutineScope.launch {
try { doSomethingAndThrow() } catch(exception: Exception) { Log.d("MyException got caught") }
}
Is the exception thrown in the suspended fun doSomethingAndThrow(…) caught by my try/catch block or it gets caught through the CoroutineScope hierarchy.
What would be a good practice to catch this exception?Animesh Sahu
08/07/2019, 3:35 AMhyunghui
08/07/2019, 10:16 AMrx's take(1)
with coroutine. Is there a good way to do it? for example, run 10 random jobs and wait only until the fastest job finishes.Dias
08/07/2019, 12:36 PMRuckus
08/07/2019, 2:18 PMkotlinx-coroutines-javafx
to provide helpers for common patterns, or is it only meant to provide a UI context?aerb
08/07/2019, 3:11 PMaerb
08/07/2019, 3:12 PMio
or main
for example?fangzhzh
08/07/2019, 9:44 PMkotlin
//
var subject = PublishSubject.create<Location>()
subject
.buffer(1L, SECONDS)
.subscribe( {
list ->
list.lastOrNull()?.let {}
}, {}, {})
The subscribe's on Next is called every 1 second, and it process only the first one. This is helpful that I have too many log and I just want print every seconds
How I do it in coroutines? ThanksWilson Castiblanco
08/07/2019, 11:20 PMDeepLinkPresenter
. Which of those are the "best" approach having in mind the CoroutineScope implementation.addamsson
08/08/2019, 9:11 AMsam
08/08/2019, 7:44 PMwhile (true) { consumer.take() }
, where consumer.take is a blocking call on a JMS client. Normally, I’d launch a new thread for this, and then shut down the thread once the program exits.Marko Mitic
08/08/2019, 9:06 PMgroostav
08/08/2019, 9:36 PMJob
and parent-child'ing.
suspend fun doLongRunningWork {
val myContext = coroutineContext
requireNotNull(myContext[Job])
val async1 = someScope.async {
//...
}
val async2 = someScope.async(myContext) {
//...
}
//maybe this code joins on async1 & async2, maybe not. should that impact my approach?
}
the latter exerts a rather strange back-pressure on the callers coroutine-entrypoint. Why the distinction?rook
08/08/2019, 9:44 PMsuspend
function execute its contents on a specific dispatcher. So far:```
suspend fun getFoo(): Foo? = coroutineScope {
withContext(Dispatchers.IO) { retrieveFoo() }
}```
Is this the best way to go about it?jkbbwr
08/09/2019, 12:43 PMAslam Hossin
08/09/2019, 4:40 PMkevinherron
08/09/2019, 5:08 PMkevinherron
08/09/2019, 5:09 PMVsevolod Tolstopyatov [JB]
08/09/2019, 6:32 PMkotlinx.coroutines
1.3.0-RC2 is here!
Changelog:
* New flow operator families
* combine
and combineTransform
* flatMapLatest
, mapLatest
, transformLatest
and collectLatest
* R8-friendly ServiceLoader
usage to enable SL optimization on Android toolchain 3.6.0+
* Stacktrace recovery for minified Android builds
* Full-blown interop between coroutine context and Reactor context
* Various fixes and improvements
* Full changelog: https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.3.0-rc2bogdoll
08/10/2019, 8:27 AMsuspend fun <T> CompletableFuture<T>.suspending() : T = suspendCoroutine { cont ->
whenComplete { v, e -> if(e==null) cont.resume(v) else cont.resumeWithException(e) }
}
It can be used for example to wrap the following AWS SDK method:
CompletableFuture<CopyObjectResponse> copyObject(CopyObjectRequest copyObjectRequest)
into
suspend fun S3AsyncClient.suspendingCopyObject(copyObjectRequest: CopyObjectRequest) : CopyObjectResponse = copyObject(copyObjectRequest).suspending()
voddan
08/10/2019, 10:17 AMBOM
in the new kotlinx-coroutines-bom
stand for?sjthn
08/10/2019, 5:38 PMprivate val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
presenter = ProductPresenter(ProductRepository(Network(), Database()), scope)
button_fetch.setOnClickListener {
presenter.onFetchClick()
}
}
---------------------------------------------------------------------
fun onFetchClick() {
loadingState.value = true
scope.launch {
//...
}
}
Robert Menke
08/11/2019, 1:43 AMKulwinder Singh
08/11/2019, 9:39 AMphoneAuth.verifyPhoneNumber(
number, 1, TimeUnit.MINUTES,
executor or Activity // but here i don't wanna scope it to activity instead i want to use it inside ViewModel using coroutines
,authCallback)
dharrigan
08/11/2019, 12:14 PMdharrigan
08/11/2019, 12:15 PMdharrigan
08/11/2019, 12:15 PMfun doSomething() = GlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
writeIntoTable1()
writeIntoTable2()
writeIntoTable3()
}
suspend fun writeIntoTable1() {...}
suspend fun writeIntoTable2() {...}
suspend fun writeIntoTable3() {...}
dharrigan
08/11/2019, 12:15 PMfun doSomething() = GlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
writeIntoTable1()
writeIntoTable2()
writeIntoTable3()
}
suspend fun writeIntoTable1() {...}
suspend fun writeIntoTable2() {...}
suspend fun writeIntoTable3() {...}
gildor
08/11/2019, 2:29 PMcoroutineScope {}
to group writes, so client could launch this suscpend function in own scope using launch, or just call suspend function (which is even better)
3. To make parallel version of this code less awkward I would use something like:
listOf(::writeIntoTable1, ::writeIntoTable2, ::writeIntoTable3).forEach { launch ( it() } }
Or any other way to group operations and do not copy paste invocation strategy
4. i see that you use IO dispatcher which looks suspicious in this case, because you call suspend function which itself never should block, so if write is blocking, just move this IO dispatcher to those functions or make them blocking explicitly, remove suspend modifier and add Blocking suffixdharrigan
08/11/2019, 3:51 PMDico
08/11/2019, 10:33 PMgildor
08/12/2019, 3:50 AMwriteIntoTableN
is blocking better to remove suspend modifier which is probably useless and rather confusing for blocking function and rename it to writeIntoTableBlocking, so it will be clear that function is blocking and should be called from corresponding contextDico
08/12/2019, 9:38 AM