Search code examples
kotlin-multiplatform

KMM and preview for swift ui


Trying out kmm with a non trivial project,

The task is to show a list of repos, and I want to have a preview of the screen in swift-ui.

I have a viewModel that get's injected a repository that will eventually query github api. For the preview I inject a fake repository that should answer with a list of hardcoded objects.

The code is in this PR https://github.com/alexandru-calinoiu/SharedAppSample/pull/1

Preview for RepoListView will crash:

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Incident Identifier: 4990E330-CD93-4D80-92C9-1426BBC3FBBA
CrashReporter Key:   E30E0C83-3AF1-4C22-98AC-D674ED184D1C
Hardware Model:      Macmini8,1
Process:             iosApp [41515]
Path:                /Users/USER/Library/Developer/Xcode/UserData/Previews/Simulator Devices/0DF05C52-98D2-4DF5-AA63-6F22DBFE76C1/data/Containers/Bundle/Application/461BB8B1-6BFE-4BB1-B1FD-0882D664AE26/iosApp.app/iosApp
Identifier:          orgIdentifier.iosApp
Version:             1.0 (1)
Code Type:           X86-64 (Native)
Role:                Foreground
Parent Process:      launchd_sim [16338]
Coalition:           com.apple.CoreSimulator.SimDevice.0DF05C52-98D2-4DF5-AA63-6F22DBFE76C1 [86637]
Responsible Process: SimulatorTrampoline [670]

Date/Time:           2022-10-10 17:12:15.1370 +0300
Launch Time:         2022-10-10 17:12:14.5247 +0300
OS Version:          macOS 12.5.1 (21G83)
Release Type:        User
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000118
Exception Codes: 0x0000000000000001, 0x0000000000000118
VM Region Info: 0x118 is not in any region.  Bytes before following region: 140737487011560
      REGION TYPE                    START - END         [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      VM_ALLOCATE              7fffffeb8000-7fffffeb9000 [    4K] r-x/r-x SM=ALI  
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [41515]

Triggered by Thread:  0

Thread 0 Crashed::  Dispatch queue: BSXPCCnx:com.apple.dt.xcode-previews.systemservices (BSCnx:client:com.apple.dt.uv.agent-preview-nonui-service)
0   RepoListView.1.preview-thunk.dylib         0x13d1f7d7b Kotlin_Interop_CreateKStringFromNSString + 107
1   iosApp                                 0x10eb2c3cd Kotlin_ObjCExport_refFromObjC + 61
2   iosApp                                 0x10ea5124f objc2kotlin.1693 + 335
3   iosApp                                 0x10e67ec70 @nonobjc ExploreSharedRepo.init(owner:name:description:primaryLanguage:lastActivity:) + 160
4   iosApp                                 0x10e67e325 ExploreSharedRepo.__allocating_init(owner:name:description:primaryLanguage:lastActivity:) + 133
5   iosApp                                 0x10e67e1ba one-time initialization function for repos + 218 (RepoListView.swift:61)
6   libdispatch.dylib                      0x110f95a3a _dispatch_client_callout + 8
7   libdispatch.dylib                      0x110f96db3 _dispatch_once_callout + 20
8   iosApp                                 0x10e67e361 Screen_Previews.repos.unsafeMutableAddressor + 49 (RepoListView.swift:60)
9   iosApp                                 0x10e67e3bb one-time initialization function for viewModel + 27 (RepoListView.swift:65)
10  libdispatch.dylib                      0x110f95a3a _dispatch_client_callout + 8
11  libdispatch.dylib                      0x110f96db3 _dispatch_once_callout + 20
12  iosApp                                 0x10e67e421 Screen_Previews.viewModel.unsafeMutableAddressor + 49 (RepoListView.swift:65)
13  RepoListView.1.preview-thunk.dylib         0x13d55de5f static Screen_Previews.__preview__previews.getter + 31 (RepoListView.swift:68)
14  iosApp                                 0x10e67e6a9 protocol witness for static PreviewProvider.previews.getter in conformance Screen_Previews + 9
15  SwiftUI                                0x1152b92a3 0x1148eb000 + 10281635
16  iosApp                                 0x10e67e6e5 protocol witness for static _PreviewProvider._previews.getter in conformance Screen_Previews + 37
17  SwiftUI                                0x1152b9ac1 0x1148eb000 + 10283713
18  PreviewsInjection                      0x10fba27f9 0x10fb78000 + 174073
19  PreviewsInjection                      0x10fba26e9 0x10fb78000 + 173801
20  PreviewsInjection                      0x10fb9991a 0x10fb78000 + 137498
..... (char limit was hit, so no full message)

By reading the log I guess it has something to do with converting strings from to kotlin, but what?


Solution

  • Try adding isStatic = false to your framework block here

    The SwiftUI preview doesn't work with static frameworks produced by Kotlin. There's some discussion about it here