https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
p

Paul Woitaschek

07/01/2021, 12:18 PM
Is there any sane way to let ios implement a suspending interface? When we have an interface that suspends:
Copy code
public fun runTest(suspending: SuspendingInterface) {
  CoroutineScope(Dispatchers.Default).launch {
    println(suspending.value())
  }
}

public interface SuspendingInterface {

  public suspend fun value(): Int
}
And ios switches threads:
Copy code
class SuspendingInterfaceImpl : SuspendingInterface{
  func value(completionHandler: @escaping (KotlinInt?, Error?) -> Void) {
    DispatchQueue.global().async {
      completionHandler(42, nil)
    }
  }
}

CoroutineTest.runTest(SuspendingInterfaceImpl())
That directly fails with:
Copy code
kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared kotlinx.coroutines.internal.DispatchedContinuation@1f67dc8 from other thread
👀 1
b

borisdamato

07/01/2021, 12:24 PM
p

Paul Woitaschek

07/01/2021, 12:24 PM
Yep
j

Johan Nenzén

07/05/2021, 6:21 PM
p

Paul Woitaschek

07/05/2021, 6:22 PM
I don't think so
To me this pretty much looks like a bug
j

Johan Nenzén

07/05/2021, 6:24 PM
Does it work if you wrap the entire call to 'value' in a dispatchqueue? Just making sure that the entire method is run on the same thread?
p

Paul Woitaschek

07/05/2021, 7:05 PM
It's an example showing that suspend functions on iOS are heavily limited because you can't switch threads
j

Johan Nenzén

07/05/2021, 8:05 PM
I won't argue with you about that. 😕
2 Views