question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default dispatcher and UI dispatcher support for iOS

See original GitHub issue

Hi, I am using 'org.jetbrains.kotlinx:kotlinx-coroutines-core-native:0.24.0' with outputKinds = [FRAMEWORK] to generate framework targeting ios_x64

I wrote some uint tests for the code which contains launch call and ran tests with Gradle without any problem, but I got this error when ran the code in iOS emulator.

There is no event loop. Use runBlocking { … } to start one

Do we now have default dispatcher and UI dispatcher support for iOS?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:19
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

37reactions
brettwilliscommented, Nov 19, 2018

@kamerok , below is the implementation I’m currently using. Updated for coroutines 1.0 and implemented the delay parts, so you don’t have to remove delay() calls.

@UseExperimental(InternalCoroutinesApi::class)
private object MainLoopDispatcher: CoroutineDispatcher(), Delay {

    override fun dispatch(context: CoroutineContext, block: Runnable) {
        dispatch_async(dispatch_get_main_queue()) {
            try {
                block.run()
            } catch (err: Throwable) {
                logError("UNCAUGHT", err.message ?: "", err)
                throw err
            }
        }
    }



    @InternalCoroutinesApi
    override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeMillis * 1_000_000), dispatch_get_main_queue()) {
            try {
                with(continuation) {
                    resumeUndispatched(Unit)
                }
            } catch (err: Throwable) {
                logError("UNCAUGHT", err.message ?: "", err)
                throw err
            }
        }
    }

    @InternalCoroutinesApi
    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()) {
            try {
                if (!handle.disposed) {
                    block.run()
                }
            } catch (err: Throwable) {
                logError("UNCAUGHT", err.message ?: "", err)
                throw err
            }
        }

        return handle
    }

}
3reactions
qwwdfsadcommented, Nov 23, 2021

Fixed with 1.6.0-RC and new memory model: when it’s enabled, global_queue is used for Dispatchers.Default and main queue for Dispatchers.Main

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dispatching Intents to Handlers - Apple Developer
Overview. When a user makes a request of your app as an intent, SiriKit needs a handler that conforms to the corresponding intent...
Read more >
Kotlin Multiplatform. Practical multithreading (part 2) - Medium
Dispatchers.Default is a default dispatcher for Coroutines mechanism: CoroutineDispatcher uses specified fabric MainDispatcherLoader under the ...
Read more >
Dispatchers.Main - Kotlin
A coroutine dispatcher that is confined to the Main thread operating with UI objects. Usually such dispatchers are single-threaded.
Read more >
Difference between usage of Dispatcher IO and Default
The difference is that Dispatchers.Default is limited to the number of CPU cores (with a minimum of 2) so only N (where N...
Read more >
Kotlin Multiplatform. Practical multithreading (part 2)
Tagged with kotlin, mobile, ios, android. ... Default is a default dispatcher for Coroutines mechanism:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found