rogeralsing
12/01/2018, 8:04 PMTolriq
12/01/2018, 9:01 PMPablo Costa
12/02/2018, 12:14 PMStuffRepo.kt
stuffDS.loadStuff (object: LoadStuffCallbacks {
override fun onSuccess(stuff : Stuff) {
// stuff ready
}
override fun onError() {
// error handling
}
})
StuffDS.kt
fun loadStuff(callbacks : LoadStuffCallbacks) {
val stuff = dao.loadStuff()
if (stuff != null) callbacks.onSuccess(stuff)
else callbacks.onError()
}
interface LoadStuffCallbacks {
fun onSuccess(stuff : Stuff)
fun onError()
}
I'm trying to replicate exactly same behaviour eliminating completely the callbacks. Is it possible to do it? What's the cleanest way of doing this?
Thanks a lot!gabin
12/02/2018, 12:32 PMDmytro Danylyk
12/03/2018, 5:08 AMenleur
12/03/2018, 12:46 PMgotoOla
12/03/2018, 4:31 PMDiefferson
12/03/2018, 5:44 PMjonathan
12/04/2018, 1:48 AMclass TSCompiler {
private val dispatcher = Executors.newFixedThreadPool(24).asCoroutineDispatcher()
public suspend fun compile(program: Any) = coroutineScope {
launch(dispatcher) {
// do something that takes awhile!
}
}
}
Would it be better in this case to just use the Executor directly, rather than using it as a coroutine Dispatcher?zokipirlo
12/04/2018, 8:50 AMRazvan
12/04/2018, 8:51 AMaaverin
12/04/2018, 10:59 AMasSingle(CommonPool)
In a unit-test class, I have 2 different tests. Each one of them uses test()
method from RxJava2 to assert the value
Problem is, every time I run my tests, on of them fails, but only one of them. If I comment away one of the tests – the other one always passes.
It feels like some threading issue, anybody had similar problems?mingkangpan
12/04/2018, 12:10 PMfun main() {
runBlocking {
val foo1 = withContext(IO) {
delay(100)
"foo1"
}
val fooDeferred = async(IO) {
delay(100)
"foo2"
}
val foo2 = fooDeferred.await()
print("$foo1 and $foo2")
}
}
^ can someone tell me, what the different here between async(IO)
and withContext(IO)
?
only that async is deferred?quiro
12/04/2018, 12:34 PMCaused by java.lang.NoSuchFieldException: _decision
at java.lang.Class.getDeclaredField(Class.java:929)
at java.util.concurrent.atomic.AtomicIntegerFieldUpdater$AtomicIntegerFieldUpdaterImpl.(AtomicIntegerFieldUpdater.java:251)
at java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdater.java:49)
at kotlinx.coroutines.AbstractContinuation.(SourceFile)
at kotlinx.coroutines.JobSupport.awaitSuspend(SourceFile:1095)
at kotlinx.coroutines.JobSupport.awaitInternal$kotlinx_coroutines_core(SourceFile:1086)
at kotlinx.coroutines.DeferredCoroutine.await$suspendImpl(SourceFile:99)
at kotlinx.coroutines.DeferredCoroutine.await(SourceFile)
at com.busuu.android.business.AppboyUtils$forceRegistration$1.invokeSuspend(SourceFile:23)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(SourceFile:32)
at kotlinx.coroutines.DispatchedTask.run(SourceFile:236)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5938)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
andi
12/04/2018, 1:53 PMGta
12/04/2018, 2:57 PMczyzby
12/04/2018, 3:27 PMCoroutineContext
?bjartek
12/04/2018, 5:14 PMkevin.cianfarini
12/04/2018, 6:40 PMdewildte
12/04/2018, 10:12 PMnulldev
12/05/2018, 8:39 AMsuspend val size: Long
get() = remoteServer.getFileSize(this)
Instead, I'm stuck with this:
suspend fun getSize() = remoteServer.getFileSize(this)
So users have to do file.getSize()
instead of just file.size
😞
There's a discussion in the forums about this but there has been no interaction from the JetBrains team. Are there any plans to support this?streetsofboston
12/05/2018, 2:39 PMJob
forever - the only way to complete the Job
would be through cancellation of its scope.
I used the delay(Long.MAX_VALUE)
to accomplish this, but this seems wrong.
Basically, the function below returns a channel that keeps sending location-updates until its enclosing scope gets cancelled.
override fun CoroutineScope.publishLocations(): ReceiveChannel<Pair<Double, Double>> =
Channel<Pair<Double, Double>>(capacity = Channel.CONFLATED).apply {
val locationListener = locationListener {
launch { send(it.latitude to it.longitude) }
}
launch {
// locationListener keeps being called each time the phone's
// location changes, and the locations are sent to `this` Channel over and over again.
locationManager.requestLocationUpdates(5_000L, 0f, CRITERIA, locationListener, null)
delay(Long.MAX_VALUE) // suspend forever ...
}.invokeOnCompletion {
// .. until the Job (its outer CoroutineScope) is cancelled.
// Close this Channel
close(it)
// And stop the locationManager from sending additional updates.
locationManager.removeUpdates(locationListener)
}
}
Is there a better alternative to creating a Job
that suspends forever until it’s cancelled?Slava Glushenkov
12/05/2018, 4:36 PMGlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
textView.text = "New Text"
}
hallvard
12/05/2018, 4:58 PM<http://Dispatchers.IO|Dispatchers.IO>
is a single threaded dispatcher, I believe, and the single thread in there is the IO thread. So the error is in the expectation.withoutclass
12/05/2018, 6:56 PMcoroutineScope{}
style of method2
, sending is always suspended waiting on a receiver, but method1
does not get suspended(blocked).
As far as I can tell, both method 1 and method2 would launch using the same dispatcher (Dispatchers.IO), and that I'm not doing anything "wrong" per se, but clearly I am given I would expect method1
to not get stuck.rocketraman
12/06/2018, 12:18 AMpublic inline fun CharSequence.replace(regex: Regex, noinline transform: (MatchResult) -> CharSequence): String
from the stdlibgroostav
12/06/2018, 7:50 AMmboudraa
12/06/2018, 2:59 PMstartWith
extension on ReceiveChannel
and i was wondering which is the best way to do that.
I have currently 2 implementations:
suspend fun <A> ReceiveChannel<A>.startWith(element: A): ReceiveChannel<A> {
val channel = Channel<A>()
channel.offer(element)
consumeEach { channel.send(it) }
return channel
}
suspend fun <A> ReceiveChannel<A>.startWith(element: A, coroutineScope: CoroutineScope): ReceiveChannel<A> {
return coroutineScope.produce {
offer(element)
this@startWith.consumeEach { this@produce.send(it) }
}
}
I’m not sure what’s the difference is between the 2 and which is the best approachzokipirlo
12/06/2018, 3:36 PMadd
, remove
) on MutableList
only in actor
which runs in Dispatcher.MAIN
, is it safe to call contains
in other context (<http://Dispatcher.IO|Dispatcher.IO>
) or should rather check that in withContext(Main)
before sending to actor
?zokipirlo
12/06/2018, 3:42 PMzokipirlo
12/06/2018, 3:42 PMdave08
12/07/2018, 3:49 AMzokipirlo
12/07/2018, 7:20 AMdave08
12/07/2018, 8:56 AMzokipirlo
12/07/2018, 10:54 AM