Hi, I’m trying to call a kotlin code from ios non ...
# multiplatform
g
Hi, I’m trying to call a kotlin code from ios non main thread and I’m getting the following error:
Copy code
########### make call
first
second
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared AsyncCalc.$makeCall$lambda-0$FUNCTION_REFERENCE$0@9d0328 from other thread
Kotlin Code:
Copy code
interface ApiCall {
    fun get(url: String, success: (String) -> Unit)
}

class AsyncCalc(private val apiCall: ApiCall) {

    fun makeCall() {
        apiCall.freeze()
        println("########### make call")
        apiCall.get("<http://www.example.com|www.example.com>") {
            println("########### $it")
        }.freeze()
    }
}
iOS impl:
Copy code
let queue = DispatchQueue(label: "com.test.queue")

class ApiCalcImpl: ApiCall {
    func get(url: String, success: @escaping (String) -> Void) {
        print("first")
        queue.async {
            print("second")
            success("hello")
            print("third")
        }
    }
}

@main
internal class AppDelegate: UIResponder, UIApplicationDelegate {
    let test = ApiCalcImpl()

    internal func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        AsyncCalc(apiCall: test).makeCall()

        return true
    }
...
k
try this:
Copy code
apiCall.get("<http://www.example.com|www.example.com>", {
            println("########### $it")
        }.freeze())
g
oh, thanks, that’s helped
k
this is the kind of BS you have to deal with with K/N
if you can manage to handle all concurrency within your kotlin module using coroutines-native-mt, you'll be better off. it will freeze the appropriate thing for you. you just have to ensure that the right things support being frozen.