Thread
#multiplatform
    h

    Henrik

    2 years ago
    Question about shared state in Android/iOS MPP. I have a working setup with compilation to Android and iOS, but now I have run into the whole frozen state world (new to me) and is stuck after some hours of trawling the web for info and examples. Simplified, I have a static global variable CurrentConfig which points to the current version of a parsed JSON config. This is used in many places of the app, so they should not load and parse it all the time on their own, but share it. Once in a while, the config is replaced with a freshly downloaded one on the fly. I guess this is an often seen pattern, but I have no idea how to solve it using Kotlin Native. Anyone have a best practice example to accomplish something like this?
    Olenyov Kirill

    Olenyov Kirill

    2 years ago
    The same situation, looking for some common caching solutions..
    p

    Piasy

    2 years ago
    In Android, you don't need to freeze it, so no special care is needed. In iOS, you must freeze it, then you can use
    AtomicReference
    , and call
    compareAndSet
    when you want to update it.
    Olenyov Kirill

    Olenyov Kirill

    2 years ago
    Is there any kind of ConcurrentHashMap for K/N?
    p

    Piasy

    2 years ago
    I think there isn't.
    h

    Henrik

    2 years ago
    Thanks Piasy. Do I need to add some kind of reference or library to Gradle to use AtomicReference? I can't resolve it. And can I use it on all platforms? The code is a shared library, so it would be nice if it worked in Android too?
    p

    Piasy

    2 years ago
    You don't need.
    AtomicReference
    can only be accessed in Kotlin Native.
    h

    Henrik

    2 years ago
    OK, thanks. Will look into it and give it a go
    I keep getting
    Unresolved reference: AtomicReference
    , even if I only use it in iOS-specific code and build for IosX64:
    val testRef = AtomicReference(someData)
    p

    Piasy

    2 years ago
    Do you add
    import kotlin.native.concurrent.AtomicReference
    ?
    h

    Henrik

    2 years ago
    Argh, sorry. I have been spending too much time with C# lately, so I only had
    import kotlin.native.concurrent
    That seems to do the trick, thank you again!
    kpgalligan

    kpgalligan

    2 years ago
    There are multiple options for caches. Check out https://github.com/touchlab/Stately
    There’s a common definition of AtomicReference, as well as a concurrent hash map, although I’m rewriting that because the implementation isn’t really performant (rewriting right now, actually)
    For a simple parsed json cache, either AtomicReference, or if you know all reads will happen on, say, the main thread, you could mark the object @ThreadLocal
    See https://dev.to/touchlab/practical-kotlin-native-concurrency-ac7 and

    https://www.youtube.com/watch?v=oxQ6e1VeH4M

    for more info
    h

    Henrik

    2 years ago
    Thanks Kevin. And thanks for some of your well explained online articles as well. Very useful information all of it 🙂 I considered
    ThreadLocal
    but I don't think it is the right fit in this context
    kpgalligan

    kpgalligan

    2 years ago
    Yeah, you need to be careful with it, and if you’re not quite sure of thread context, it’ll be bad.
    h

    Henrik

    2 years ago
    Quick question to help my understanding. If an
    AtomicReference
    is used as a private variable inside a frozen class, will it then be frozen too, so that I cannot update it using
    compareAndSet
    ?