Andy Victors
05/13/2019, 12:42 PMktor
in MPP environment, specifically on iOS where currently only Main-Thread Dispatcher is offered (https://github.com/Kotlin/kotlinx.coroutines/issues/462)?Jan Stoltman
05/13/2019, 12:43 PMribesg
05/13/2019, 12:44 PMAndy Victors
05/13/2019, 1:24 PMprivate class IODispatcher: CoroutineDispatcher() {
val queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND.toLong(), 0)
override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatch_async(queue) {
block.run()
}
}
}
actual class MainScope: CoroutineScope {
private val dispatcher = MainDispatcher()
private val job = Job()
override val coroutineContext: CoroutineContext
get() = dispatcher + job
}
Then I use
MainScope().launch { }
to execute ktor requests.
But then obviously, each request will be executed on main thread, which I do not wantribesg
05/13/2019, 1:32 PMAndy Victors
05/13/2019, 1:33 PMribesg
05/13/2019, 1:33 PMAndy Victors
05/13/2019, 1:34 PMribesg
05/13/2019, 1:35 PMAndy Victors
05/13/2019, 1:36 PMribesg
05/13/2019, 1:37 PMribesg
05/13/2019, 1:38 PMabstract class BasePresenter<T: Any>(viewRef: T) : CoroutineScope {
private var job = Job()
override val coroutineContext: CoroutineContext
get() = UI() + job
fun onDestroy() {
job.cancel()
}
}
ribesg
05/13/2019, 1:39 PMclass UI : CoroutineDispatcher(), Delay {
override fun dispatch(context: CoroutineContext, block: Runnable) {
val queue = dispatch_get_main_queue()
dispatch_async(queue) {
block.run()
}
}
@ExperimentalCoroutinesApi
override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeMillis * 1_000_000), dispatch_get_main_queue()) {
with(continuation) {
resumeUndispatched(Unit)
}
}
}
override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle {
val handle = object : DisposableHandle {
var disposed = false
private set
override fun dispose() {
disposed = true
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeMillis * 1_000_000), dispatch_get_main_queue()) {
if (!handle.disposed) {
block.run()
}
}
return handle
}
}
Andy Victors
05/13/2019, 1:43 PMsuspend fun getKPIData() : String {
val params = HashMap<String, String>()
return client.get<String> {
url(constructURL("kpis", params))
}
}
ribesg
05/13/2019, 1:48 PMlaunch { }
and call our suspend function in thereribesg
05/13/2019, 1:49 PMribesg
05/13/2019, 1:49 PMSupervisorJob
for exampleAndy Victors
05/13/2019, 1:51 PMribesg
05/13/2019, 1:54 PMSam
05/13/2019, 1:55 PMAndy Victors
05/13/2019, 1:56 PMJan Stoltman
05/13/2019, 2:03 PMAndy Victors
05/13/2019, 2:06 PMAndy Victors
05/13/2019, 2:08 PMkotlin.TypeCastException
while trying to use solution as aboveribesg
05/13/2019, 2:09 PMribesg
05/13/2019, 2:11 PMAndy Victors
05/13/2019, 2:16 PMribesg
05/13/2019, 2:18 PMribesg
05/13/2019, 2:19 PMabstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
ribesg
05/13/2019, 2:20 PMribesg
05/13/2019, 2:21 PMcoroutineContext.cancelChildren()
in your onDestroy()
rather than just cancel()
Andy Victors
05/13/2019, 2:30 PMribesg
05/13/2019, 2:32 PMribesg
05/13/2019, 2:32 PMribesg
05/13/2019, 2:34 PMribesg
05/13/2019, 2:34 PMCasey Brooks
05/13/2019, 6:27 PMCasey Brooks
05/13/2019, 6:35 PMmutation attempt of frozen io.ktor.client.request.HttpRequestPipeline
on Kotlin 1.3.31, ktor 1.1.4, coroutines 1.2.0. Are there different versions that are known to work?uli
05/13/2019, 7:12 PMCasey Brooks
05/13/2019, 7:34 PMDispatchers
, and creating it like any of the above solutions also fails (despite them saying that it works)ribesg
05/13/2019, 8:25 PMobject
(or inside an instance of something that is inside an object
, etc)Casey Brooks
05/13/2019, 8:38 PM