https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Jaroslav

02/19/2019, 6:12 AM
Any examples on how to use channels on iOS? I have a library that expects me to give a
ReceiveChannel
object, but in iOS I cannot instantiate
Kotlinx_coroutines_core_nativeReceiveChannel
because it has no accessible initializers
d

Dico

02/19/2019, 6:20 AM
You need to invoke
Channel()
function and send elements to the resulting channel, which is a SendChannel as well as a ReceiveChannel.
Keep in mind that the type of channel may be relevant
j

Jaroslav

02/19/2019, 6:27 AM
looks like Channel also doesn’t have any initializers suitable for ios 😞
Copy code
__attribute__((swift_name("Kotlinx_coroutines_core_nativeChannel")))
@protocol Eld_commonKotlinx_coroutines_core_nativeChannel <Eld_commonKotlinx_coroutines_core_nativeSendChannel, Eld_commonKotlinx_coroutines_core_nativeReceiveChannel>
@required
@end;
y

yshrsmz

02/19/2019, 9:08 AM
You can convert it to callback in Kotlin code. I think they don't meant to be touched from Swift
I'm not sure if this is the best approach, but here's my pet project. It uses Ktor & SQLDelight to fetch and cache GitHub API. It's quite messy, but it also uses Channel so may be you can grasp some idea. https://github.com/yshrsmz/GitHubKotlinMPPSample
j

Jaroslav

02/19/2019, 11:22 AM
I’ll have a look at it, thanks!
2 Views