hey folks I found several ways how to deliver the ...
# kotlin-native
i
hey folks I found several ways how to deliver the result of background task execution back to the main thread on iOS, one example shown in
DroidconKotlin
by Touchlab https://github.com/touchlab/DroidconKotlin/blob/kotlin-1.3.20/sessionize/lib/src/iosMain/kotlin/co/touchlab/sessionize/platform/Functions.kt#L49 it uses ThreadLocal wrapper. The question why not just do this instead:
Copy code
override operator fun <T> invoke(query: Database.() -> T, callback: (T) -> Unit) {
        worker.execute(TransferMode.SAFE, { QueryTask(database, query, callback).freeze() }) { task ->
            val result = task.query(task.database)
            dispatch_async_f(
                queue = dispatch_get_main_queue(),
                context = DetachedObjectGraph { QueryTaskResult(result.freeze(), task.callback) }.asCPointer(),
                work = staticCFunction { it: COpaquePointer? ->
                    initRuntimeIfNeeded()
                    val result = DetachedObjectGraph<Any>(it).attach() as QueryTaskResult<T>
                    result.callback(result.result)
                }
            )
        }
    }
Am I missing something, and there is something wrong with the code above? It seems working
y
From my understanding, if you freeze callback, everything you reference from inside callback will be frozen.
👀 1
For example you can't use coroutine from callback in your example.
i
Callback is passed from iOS VC, and inside the callback it renders some UI. So far works just fine.
And the same happens in the sample code from Touchlab,
JobWrapper(backJob, mainJobHolder).freeze()
.
o
especially
Continuator
solves problem of non-freezing the callback
y
Thanks! I'll look into it
In the meantime I wrote some quick test to check the behaviour of ThreadLocalRef. If we wrap lambda with ThreadLocalRef, then that lambda is not frozen https://gist.github.com/yshrsmz/cf9c53bac9b07b3ca02c1286ea796ad1
i
@olonho thx for the sharing examples. One more question (I’m trying to get my head around how the freeze operation works) I can see how that works for any model object graph, so everything marked to be immutable and raise exception if someone tries to modify it. But what
freeze
operation does for the closure / callback especially if it passed from let’s say swift realm, what implications of freezing passed callback?
o
Freeze doesn’t go cross-runtime, Swift/C data keeps their mutability
👍 1
k
I’m just seeing this. Will have to look in detail at the “why not just do ___“, but I wrote the droidcon code. The basic idea is that I want to pass the function to be executed when the background returns to the main thread, and to avoid freezing, that is kept in the thread it is called from and returning to (IE. the main thread). I do the same thing with a generic coroutines background processor (backgroundSuspend) and the sqldelight Pub/Sub listener in the app. The background tasks are generally frozen, but any function/lambda set and called in the main thread is kept local to the thread, and avoids freezing. I have a pending blog post to talk about this, but I have a lot of pending blog posts, and no real eta on that.
I made a video explaining better. Posting when uploaded

https://www.youtube.com/watch?v=yhvsKlxPZPQ&amp;feature=youtu.be

👏 2
i
thx @kpgalligan!!!