Hi guys, I have a question on the sample SpaceXNew...
# multiplatform
h
Hi guys, I have a question on the sample SpaceXNews App that KMM doc provides. The tutorial link is here Create a multiplatform app using Ktor and SQLDelight, source code is on github, I got a crash on iOS App when I try to test on SwiftUI. The crash happens on
RocketLaunchRow_Previews
when I try to make a
mockRocketLaunch
. It seems the
mockRocketLaunch
can not be initialised. From the crash log, I think it might be native memory model issue, however, I could not get useful information to find out the root cause. Could anyone provide more information on the crash? Thanks.
Copy code
// Crash Log 
1   libsystem_pthread.dylib       	       0x1af25a1e8 pthread_kill + 256
2   libsystem_c.dylib             	       0x180129e28 abort + 124
3   shared                        	       0x106ad9924 konan::abort() + 12
4   shared                        	       0x106ae791c (anonymous namespace)::terminateWithUnhandledException(ObjHeader*)::$_1::operator()() const + 20
5   shared                        	       0x106ae77e0 void (anonymous namespace)::$_0::operator()<(anonymous namespace)::terminateWithUnhandledException(ObjHeader*)::$_1>((anonymous namespace)::terminateWithUnhandledException(ObjHeader*)::$_1) + 80
6   shared                        	       0x106ae75a0 (anonymous namespace)::terminateWithUnhandledException(ObjHeader*) + 56
7   shared                        	       0x106ae7530 (anonymous namespace)::processUnhandledException(ObjHeader*) + 100
8   shared                        	       0x106ae93d4 kotlin::ProcessUnhandledException(ObjHeader*) + 60
9   shared                        	       0x106aeccb4 Kotlin_ObjCExport_trapOnUndeclaredException + 36
10  shared                        	       0x106a96e9c objc2kotlin.1298 + 424 (/<compiler-generated>:1)
11  RocketLaunchRow.1.preview-thunk.dylib	       0x114f2f268 @nonobjc SharedRocketLaunch.init(flightNumber:missionName:launchDateUTC:details:launchSuccess:links:) + 220
12  RocketLaunchRow.1.preview-thunk.dylib	       0x114f2dd58 SharedRocketLaunch.__allocating_init(flightNumber:missionName:launchDateUTC:details:launchSuccess:links:) + 120
13  RocketLaunchRow.1.preview-thunk.dylib	       0x114f2da74 static RocketLaunchRow_Previews.__preview__previews.getter + 940 (RocketLaunchRow.swift:53)
14  iosApp                        	       0x1049817c8 protocol witness for static PreviewProvider.previews.getter in conformance RocketLaunchRow_Previews + 12
15
Source Code
Copy code
// iosApp/iosApp/RocketLaunchRow.swift
import SwiftUI
import shared

struct RocketLaunchRow: View {
    var rocketLaunch: RocketLaunch
    
    var body: some View {
        HStack {
            VStack(alignment: .leading, spacing: 10.0) {
                Text("hello world")
            }
            Spacer()
        }
    }
}


extension RocketLaunchRow {
    private var launchText: String {
        if let isSuccess = rocketLaunch.launchSuccess {
            return isSuccess.boolValue ? "Successful" : "Unsuccessful"
        } else {
            return "No data"
        }
    }

    private var launchColor: Color {
        if let isSuccess = rocketLaunch.launchSuccess {
            return isSuccess.boolValue ? Color.green : Color.red
        } else {
            return Color.gray
        }
    }
}


struct RocketLaunchRow_Previews: PreviewProvider {
    static var previews: some View {
        let links = Links(patch: nil, article: "article")

        let mockRocketLaunch = RocketLaunch(flightNumber: 1,
                                            missionName: "Mars",
                                            launchDateUTC: "UTC+8",
                                            details: "Details",
                                            launchSuccess: true,
                                            links: links)



    }
}
k
What does
RocketLaunch
looks like? Is it a data class or more complex?
h
Hi @Kevin S, it’s defined here, shared/src/commonMain/kotlin/com/jetbrains/handson/kmm/shared/entity/Entity.kt
Copy code
package com.jetbrains.handson.kmm.shared.entity

import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class RocketLaunch(
    @SerialName("flight_number")
    val flightNumber: Int,
    @SerialName("name")
    val missionName: String,
    @SerialName("date_utc")
    val launchDateUTC: String,
    @SerialName("details")
    val details: String?,
    @SerialName("success")
    val launchSuccess: Boolean?,
    @SerialName("links")
    val links: Links
) {
    var launchYear = launchDateUTC.toInstant().toLocalDateTime(TimeZone.UTC).year
}

@Serializable
data class Links(
    @SerialName("patch")
    val patch: Patch?,
    @SerialName("article")
    val article: String?
)

@Serializable
data class Patch(
    @SerialName("small")
    val small: String?,
    @SerialName("large")
    val large: String?
)
k
The issue is with
launchYear
, since that variable is generated when the class is created, it’s using the
kotlinx datetime
package when running the preview, which is not allowed. You could instead replace it with a getter, such as:
Copy code
val launchYear:Int
        get() = launchDateUTC.toInstant().toLocalDateTime(TimeZone.UTC).year
That should work since it’s not using the
kotlinx
library until it needs to. It should work as long as you don’t reference that variable from the preview code.
h
That’s beautiful answer. Thanks so much.