Hello, I'm new to KN an I am trying to make my Mul...
# kotlin-native
a
Hello, I'm new to KN an I am trying to make my Multiplatform project run on iOS but I can't get it to work. I need to use the function from external library that takes callback in a form of lambda. When this lambda gets invoked it passes data object that I later want to set to
StateFlow
. The problem is that this lambda gets called on some other thread so I have to freeze it. But when I freeze it the data objects also gets frozen and in turn it freezes the
StateFlow
and then it crashes with mutability exception. Is it even possible to do what I described with current KN memory model? Any help would be appreciated, thanks!
m
There's a lot of information about this in this thread: https://github.com/kotlin/kotlinx.coroutines/issues/462
a
I've already read this issue but this does not really apply to my use case. I am using experimental multi threaded coroutines but it does not really matter because mt coroutines do not solve the problem of mutating values between threads.
Like detaching your object before passing it to your
StateFlow
?
a
I will look into it, thanks
k
Detaching is generally not great. We never do that anywhere in production. “mt coroutines do not solve the problem of mutating values between threads” You basically can’t do this without a lot of hassle, but in general, you don’t need to. What’s being frozen that you don’t want frozen?
m
Why is detaching not good? Memory leaks?
k
It’s syntactically difficult, for one. I wrote about it a while back: https://dev.to/touchlab/kotlin-native-transferring-state-4n8i
Copy code
@Test
    fun failLocal(){
        val d = Dat("Hello")
        assertFails {
            DetachedObjectGraph {d}
        }
    }

    data class Dat(val s:String)
You can’t just detach it and call a background thread. You need to somehow detach state without holding a reference to it. It’s tricky
We’ve also seen issues where state is detached, but there was a runtime issue related to the memory checker. That may be fixed, but at the time, you needed to call GC explicitly (IIRC).
All of the use cases for which I thought it would be necessary have turned out not to be. Architecting around it is generally straightforward. Granted, most of the work we’ve done has been on newer code, not refactoring significant existing code. Anyway…
m
I see, thanks for the explanation! I actually thought I was using it somewhere but can't find it anymore 🙂
k
There’s a performance consideration. One of the major reasons people want mutable state between threads is for a cache. Say you have a map with 10k items. Every time you access it, you need to attach, do something, then detach. Every time you detach, the runtime looks at every element in that structure. So, in Big O terms, a map with constant time lookup is suddenly N.
You would also need to put a lock around that whole operation to avoid 2 threads trying to attach at the same time, but that’s the lesser of the concerns.
We simply don’t use it, ever.
👍 1
a
@kpgalligan Hi, sorry i did not log in here for a while and did not see your follow up. After research i’ve also came to conclusion that even though it works
DetachedObjectGraph
is very wasteful. My issue was that i want to get something from the background thread and then post the value to the
StateFlow
that was created in the UI thread. Right now it fails with freezing exception. I’ve managed to find a workaround to use
ConflatedBroadcastChannel
which somehow works in this situation. But i wonder is it somehow possible to do this with
StateFlow
or am I approaching this issue from the wrong angle?