https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
h

Henrik

01/24/2020, 7:32 AM
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?
o

Olenyov Kirill

01/24/2020, 8:46 AM
The same situation, looking for some common caching solutions..
p

Piasy

01/24/2020, 8:47 AM
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.
o

Olenyov Kirill

01/24/2020, 8:57 AM
Is there any kind of ConcurrentHashMap for K/N?
p

Piasy

01/24/2020, 9:02 AM
I think there isn't.
h

Henrik

01/24/2020, 9:03 AM
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

01/24/2020, 9:04 AM
You don't need.
AtomicReference
can only be accessed in Kotlin Native.
h

Henrik

01/24/2020, 9:05 AM
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

01/24/2020, 10:10 AM
Do you add
import kotlin.native.concurrent.AtomicReference
?
h

Henrik

01/24/2020, 10:16 AM
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!
k

kpgalligan

01/24/2020, 12:43 PM
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

01/24/2020, 12:47 PM
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
k

kpgalligan

01/24/2020, 12:50 PM
Yeah, you need to be careful with it, and if you’re not quite sure of thread context, it’ll be bad.
h

Henrik

01/24/2020, 2:09 PM
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
?
5 Views