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

sashjakk

01/04/2019, 12:10 PM
Hey, would be nice if somebody could point what I am doing wrong 😅 trying to get mpp common module working on iOS. Able to run it on Android, however iOS showing
kotlin.TypeCastException
🤨 Common part:
Copy code
@Serializable
data class Post(
    val userId: Long,
    val id: Long,
    val title: String,
    val body: String
)

class KtorClient : CoroutineScope {
    private val httpClient = HttpClient()

    override val coroutineContext: CoroutineContext
        get() = Job()

    fun getPosts(callback: (String, Throwable?) -> Unit) {
        launch {
            try {
                val raw = httpClient.get<String> {
                    url("<https://jsonplaceholder.typicode.com/posts>")
                }
                println(raw)
                callback(raw, null)
            } catch (e: Throwable) {
                callback("", e)
            }
        }
    }
}
iOS part:
Copy code
let client = KtorClient()
        client.getPosts { (posts: String, error: KotlinThrowable?) -> KotlinUnit in
            if (error != nil) {
                error?.printStackTrace()
                print(error?.cause ?? "oups")
                print(error?.message ?? "oups")
                return KotlinUnit()
            }
            
            print(posts)
            
            return KotlinUnit()
        }
d

Dico

01/04/2019, 12:33 PM
Stacktrace?
s

sashjakk

01/04/2019, 12:35 PM
The only thing I got for now…
d

Dico

01/04/2019, 12:37 PM
Maybe it's
error?.cause ?? "oups"
s

sashjakk

01/04/2019, 12:44 PM
does not seem so, I think it comes from common module, empty closure on ios side, produces the same
s

Sam

01/04/2019, 12:54 PM
It’s early in the morning for me but I think your Swift get client line just be:
client.getPosts {posts, error -> KotlinUnit in
s

sashjakk

01/04/2019, 12:58 PM
yeap, tried like that as well… same error
c

coletz

01/04/2019, 5:12 PM
can you share your gradle config? maybe you are using conflicting/old version of some lib
s

sashjakk

01/04/2019, 7:23 PM
Copy code
sourceSets {
        commonMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.9.1"
                
                implementation "io.ktor:ktor-client-core:$ktor_version"
                implementation "io.ktor:ktor-client-json:$ktor_version"
            }
        }
        commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-common'
                implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
            }
        }
        androidMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib'
                implementation "io.ktor:ktor-client-android:$ktor_version"
                implementation "io.ktor:ktor-client-json-jvm:$ktor_version"
                implementation "io.ktor:ktor-client-okhttp:$ktor_version"

            }
        }
        androidTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test'
                implementation 'org.jetbrains.kotlin:kotlin-test-junit'
            }
        }
        iosMain {
            dependencies {
                implementation "io.ktor:ktor-client-ios:$ktor_version"
                implementation "io.ktor:ktor-client-core-native:$ktor_version"
//                implementation "io.ktor:ktor-client-json-native:$ktor_version"
                
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.9.1"

            }
        }
        iosTest {
        }
    }
Copy code
buildscript {
    ext {
        kotlin_version = '1.3.11'
        ktor_version = '1.1.1'
        coroutines_version = '1.1.0'
    }

    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
    }
}
repositories {
    google()
    jcenter()
}
@coletzcurrently my gradle setup looks like this
Ok, seems like I got it… I was missing dispatcher in coroutine context on iOS
In my case it should be like:
Copy code
override val coroutineContext: CoroutineContext
        get() = PlatformDispatcher + Job()
Copy code
expect object PlatformDispatcher: CoroutineDispatcher
and naive iOS dispatcher:
Copy code
actual object PlatformDispatcher: CoroutineDispatcher() {
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        dispatch_async(dispatch_get_main_queue()) {
            block.run()
        }
    }
}
2 Views