I'm still relatively new to KMM and a project I'm working on keeps yeilding this error:
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared dev.mrbe.haarShared.HaarSDK@abc468 from other thread
at 0 kotlinmultiplatformsharedmodule 0x00000001020909a1 kfun:kotlin.Throwable#<init>(kotlin.String?){} + 97 (/opt/buildAgent/work/c5a36d4d82b914cf/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt:24:56)
at 1 kotlinmultiplatformsharedmodule 0x0000000102089dad kfun:kotlin.Exception#<init>(kotlin.String?){} + 93 (/opt/buildAgent/work/c5a36d4d82b914cf/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:23:58)
at 2 kotlinmultiplatformsharedmodule 0x0000000102089fcd kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 93 (/opt/buildAgent/work/c5a36d4d82b914cf/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:34:58)
at 3 kotlinmultiplatformsharedmodule 0x00000001020c140d kfun:kotlin.native.IncorrectDereferenceException#<init>(kotlin.String){} + 93 (/opt/buildAgent/work/c5a36d4d82b914cf/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/Runtime.kt:31:50)
at 4 kotlinmultiplatformsharedmodule 0x00000001020c508f ThrowIllegalObjectSharingException + 623 (/opt/buildAgent/work/c5a36d4d82b914cf/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:115:11)
at 5 kotlinmultiplatformsharedmodule 0x00000001022a1b52 _ZN12_GLOBAL__N_128throwIllegalSharingExceptionEP9ObjHeader + 34
at 6 kotlinmultiplatformsharedmodule 0x00000001022a389d _ZN12_GLOBAL__N_136terminateWithIllegalSharingExceptionEP9ObjHeader + 13
at 7 kotlinmultiplatformsharedmodule 0x00000001022a39b5 _ZNK27BackRefFromAssociatedObject3refIL11ErrorPolicy3EEEP9ObjHeaderv + 133
at 8 kotlinmultiplatformsharedmodule 0x0000000102070e25 -[KotlinBase toKotlin:] + 21
at 9 kotlinmultiplatformsharedmodule 0x0000000102295058 Kotlin_ObjCExport_refFromObjC + 88
at 10 kotlinmultiplatformsharedmodule 0x0000000102040676 objc2kotlin.505 + 230
at 11 haar 0x00000001006b3181 $s4haar11MyViewModelC10addBooking7bookingySo031KotlinmultiplatformsharedmoduleF0C_tYaFTY0_ + 273 (/Users/essienedim/Desktop/HAAR /haar/haar/ContentView.swift:422:33)
at 12 libswift_Concurrency.dylib 0x00007ff833c40426 _ZN5swift34runJobInEstablishedExecutorContextEPNS_3JobE + 310
at 13 libswift_Concurrency.dylib 0x00007ff833c411bd _ZL17swift_job_runImplPN5swift3JobENS_11ExecutorRefE + 77
at 14 libdispatch.dylib 0x00000001014dfe77 _dispatch_continuation_pop + 87
at 15 libdispatch.dylib 0x00000001014def27 _dispatch_async_redirect_invoke + 997
at 16 libdispatch.dylib 0x00000001014f1e77 _dispatch_root_queue_drain + 414
at 17 libdispatch.dylib 0x00000001014f2b17 _dispatch_worker_thread2 + 278
at 18 libsystem_pthread.dylib 0x00007ff833e39f8a _pthread_wqthread + 256
at 19 libsystem_pthread.dylib 0x00007ff833e38f57 start_wqthread + 15
CoreSimulator 857.7 - Device: iPhone 14 Pro (C24AA3AD-B79D-437E-BCB7-A15C83C0F412) - Runtime: iOS 16.0 (20A360) - DeviceType: iPhone 14 Pro
(lldb)
I have seen this and some other posts on this but when I try using the freeze() method, I keep getting an Unresolved reference: freeze. I can't seem to figure out what exactly I'm doing wrong and honestly, what I should be freezing exactly.
Here's the function that seems to be triggering this Exception:
func addBooking(booking: Booking) async {
await sdk.postBooking(booking: booking)
}
The SDK class is initialise in the view model like so:
class MyViewModel: ObservableObject {
let sdk: HaarSDK
...
init(sdk: HaarSDK) {
self.sdk = sdk
...
}
}
Main View:
struct MainView: View {
@StateObject private var viewModel: MyViewModel
init() {
let sdk = HaarSDK(databaseDriverFactory: DatabaseDriverFactory())
let viewModel = MyViewModel(sdk: sdk)
_viewModel = StateObject(wrappedValue: viewModel)
}
...
}
SDK class (Kotlin):
class HaarSDK(databaseDriverFactory: DatabaseDriverFactory) {
private val database = Database(databaseDriverFactory)
private val api = HaarApi()
...
@Throws(Exception::class)
suspend fun postBooking(booking: Booking) {
api.sendBooking(booking)
}
...
}
Database:
internal class Database(databaseDriverFactory: DatabaseDriverFactory) {
private val database by lazy {
AppDatabase(databaseDriverFactory.createDriver())
}
private val dbQuery = database.appDatabaseQueries
...
}
API class:
class HaarApi {
private val httpClient = HttpClient {
install(JsonFeature) {
val json = Json { ignoreUnknownKeys = true }
serializer = KotlinxSerializer(json)
}
...
suspend fun sendBooking(booking: Booking) {
val response: HttpResponse = httpClient.post(BOOKING_ENDPOINT) {
contentType(ContentType.Application.Json)
body = booking
}
...
}