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

dimsuz

02/07/2020, 1:02 PM
Heh, I opened this chat to ask if there's a proper DI library for KMP which is not a Service Locator implementation, and here is the survey about missing libraries. Still. Is there a non-SL DI library available for KMP?
k

Kris Wong

02/07/2020, 1:14 PM
do you need it to support concurrency?
k

kpgalligan

02/07/2020, 1:15 PM
Not that I’m aware of. What comparable libraries would you be looking for?
k

Kris Wong

02/07/2020, 1:16 PM
here is the most "authoritative" list of libraries I have come across: https://github.com/AAkira/Kotlin-Multiplatform-Libraries
d

dimsuz

02/07/2020, 1:17 PM
Not that I’m aware of. What comparable libraries would you be looking for?
I guess it would be Dagger/Toothpick. Maybe Guice, but it uses reflection which is slow on Android.
do you need it to support concurrency?
Not only that. To provide dependencies in general, this is crucial for large apps...
k

Kris Wong

02/07/2020, 1:18 PM
none of the current DI/SL libraries support native concurrency last I checked
i rolled my own barebones impl.
k

kpgalligan

02/07/2020, 1:19 PM
So, no. I’m working with Koin lead on native, obviously with concurrency support, but that’s not “proper”, so no, there are no options as of now.
We’re using the Koin version now for our stuff: https://github.com/touchlab/KaMPKit. Hopefully we’ll have a version out with 1.3.70, but we’ll see
d

dimsuz

02/07/2020, 1:20 PM
Got it! I mentioned this in the survey posted above :)
I had an experience with Kodein, so I thought to try it, it seem to have KMP support already.
none of the current DI/SL libraries support native concurrency last I checked
You mean accessing dependencies from other threads? Issues with freezing?
k

kpgalligan

02/07/2020, 1:21 PM
It does not really support native threading, unless the situation has changed.
Yes
He means that
d

dimsuz

02/07/2020, 1:22 PM
I see. Can I work around that by creating classes on the main thread?
k

kpgalligan

02/07/2020, 1:22 PM
The Koin version I’m working on would support that, although you’d need to explicitly let other threads inject. I want to avoid silently freezing if possible
Yeah, that’s the goal of what I’m working on with the Koin version. All DI is main thread, and you can keep state mutable
If you need to move it to another thread, it’s frozen
Description in readme (not exactly a full description, but you get the idea): https://github.com/kpgalligan/koin/tree/kpg/kmp_update
d

dimsuz

02/07/2020, 1:24 PM
Great, thank you!
k

kpgalligan

02/07/2020, 1:25 PM
I’m guessing there will be a serious release soon. I want to push more of an experimental one in the next week or so (“experimental” in that we also want to allow JVM to work as it currently does, optionally, but the experimental one will just be main-thread only)
👏 1
k

Kris Wong

02/07/2020, 1:27 PM
I managed to get SL using
by inject()
without having in injected object frozen
k

kpgalligan

02/07/2020, 1:30 PM
With Kodein? If yes, my issue is, as of last time I looked at it, if you happened to capture something from kodein or access it from another thread, it would (in theory) freeze the kodein config, which would silently cause issues. Maybe not a huge deal sometimes, but you’ll eventually have issues. I think it should be explicit, and I also want Android to behave the same. So, the Koin version I’m working on will throw the same exceptions if you try to access it from other threads.
k

Kris Wong

02/07/2020, 1:32 PM
no, my own
kodein just crashed
k

kpgalligan

02/07/2020, 1:33 PM
Yeah, I mean people use it. Not trashing on it. I just think the thread awareness needs to be explicit
However, I’m a little fixated on the concurrency topic
d

dimsuz

02/07/2020, 3:04 PM
I support explicitness too. I've used Toothpick DI library (jvm) and they went with ConcurrentHashMaps internally, but I never needed this thread safety in my apps, so maybe I could avoid paying performance price for this. Although, of course, I didn't notice any slowdown, so it's not fair judgement, but I still think sometimes it could be lighter.