I_am_facing__Uncaught_Kotlin_exception__kotlin_nat...
# kotlin-native
k
I_am_facing__Uncaught_Kotlin_exception__kotlin_native_IncorrectDereferenceException__illegal_attempt_to_access_non-shared__issue_when_I_access_this_piece_of_code_in_Xcode_.txt
o
What if you freeze the lambda?
k
I mean, I need some sort of callback from the dataTask right!
o
Copy code
import platform.Foundation.*
import platform.posix.*
import kotlin.native.concurrent.freeze

fun networkStatusCheck(urlAddress: String): String {
    val session = NSURLSession.sharedSession
    val url = NSURL.URLWithString(urlAddress)
    val task = session.dataTaskWithURL(url!!, {
            data:NSData?, response:NSURLResponse?, error:NSError? -> Unit
        if (error == null) {
            println("The data is : $data")
            println("Response url ---> ${response!!.URL}")
            println("The response is : $response")
        } else {
            println("error ---> $error")
        }
    }.freeze())
    task.resume()
    return ""
}

fun main() {
   println(networkStatusCheck("<http://google.com>"))
   sleep(100)
}
k
okay, let me check that out. Appreciate the help.
wow, it worked like a charm, but is it the recommended way to go?
o
yes, it is recommended way to go, as dataTaskWithURL executes callback in different thread.
k
Yeah, it executes in a different thread. So what was happening earlier that broke the code. Can you tell me how this worked?
o
lambda is an object, it can capture the state and is mutable, thus when accessed from another thread mutability checks prevents this access. After freezing object become immutable and can be accessed.
👍 1