Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay function throws TypeCastException. #920

Closed
horita-yuya opened this issue Dec 28, 2018 · 7 comments
Closed

Delay function throws TypeCastException. #920

horita-yuya opened this issue Dec 28, 2018 · 7 comments
Assignees

Comments

@horita-yuya
Copy link

horita-yuya commented Dec 28, 2018

The delay suspend function throws TypeCastException when called from Swift.

The Condition.

  • preparing CoroutineDispatcher for main queue.
  • launch coroutines using the dispatcher.

I prepared self-containing repository here.
https://github.com/horita-yuya/TempKotlinNativeReport
kotlinx.coroutines version: 1.1.0

@uncledoc
Copy link

uncledoc commented Dec 29, 2018

The same problem but I think problem is in GlobalScope. There are my steps to reproduce:
1. Create multiplatform project for Android and iOS
2. Add dependencies org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.1.0, org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0 and org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.1.0
3. Write a simple function in common or iOS module with body GlobalScope.launch { // some code or empty }
4. Try to call function in swift from iOS app

p.s. macos 10.14.2, xcode 10.1

@qwwdfsad qwwdfsad self-assigned this Jan 9, 2019
@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Jan 9, 2019

It is a small regression introduced by #860.
An actual exception should be kotlin.IllegalStateException: There is no event loop. Use runBlocking { ... } to start one. because dispatcher does not implement Delay interface.

@horita-yuya
Copy link
Author

I see. Thank you.

@ildarsharafutdinov
Copy link

ildarsharafutdinov commented Mar 1, 2019

Having that my mpp framework uses coroutines, what is the right way to "start event loop" on iOS?

What about the following? Is it a supposed way?

// somewhere in framework

fun appMain(mainBlock: () -> Unit) = runBlocking {
    mainBlock()
}
// main.m

int main(int argc, char * argv[]) {
    [MppKt appMainMainBlock:^MppKotlinUnit * {
    
        @autoreleasepool {
             result = UIApplicationMain(argc, argv, nil, @"MyAppDelegate");
        }
        return NULL;
    }];
}

@pardom
Copy link

pardom commented Mar 14, 2019

An actual exception should be kotlin.IllegalStateException: There is no event loop. Use runBlocking { ... } to start one. because dispatcher does not implement Delay interface.

Given this restriction, how should I approach using this JVM/Native API while also supporting JS targets? Should I wrap my API with an expect/actual?

@pardom
Copy link

pardom commented Mar 14, 2019

Not sure if this is my issue exactly, but I was linked here from JetBrains/kotlin-native#2752 in response to a bug manifested in oolong-kt/oolong#16. The project does not call delay as @qwwdfsad mentions above, but it also doesn't call runBlocking because I have no need to block and I also want to support all platforms.

@elizarov
Copy link
Contributor

@pardom You don't need to block if you are using coroutines on the main (UI) thread in iOS, but you'll need to write the corresponding dispatcher. See here: #470 Does it help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants