Hey there! I am currently building an Compose Mul...
# multiplatform
o
Hey there! I am currently building an Compose Multiplatform app that utilizes an MQTT client to pull some data and display it in a pretty way. Since the library (KMQTT, https://github.com/davidepianca98/KMQTT) has very little documentation, I wanted to simply play around with it and test it. When I try to run my code on Android however, the App immediately crashes, as I encounter an
android.os.NetworkOnMainThreadException
error. All online suggestions I have found to fix this issue use the
thread { }
block in some capacity - as far as I know, I cannot use threads in my multiplatform project, as this block from the Kotlin standard library is only supported for JVM targets. Since I am new to App development and Android app development and Kotlin and KMP, I for sure cannot rule out that I am just doing something wrong. Has anyone here used that library before and is able to help me out here? Thanks!
c
What about using an async block with
<http://Dispatchers.IO|Dispatchers.IO>
?
o
Also seems to be JVM-only
c
It works on Android
o
Maybe my initial question or problem was phrased wrong - I was looking for a multiplatform solution to the problem. Not a solution that only works on Android, where I have to write several implementations for each platform.
c
Isn't that what multiplatform is? A per platform solution that exposed the abstraction in common? I'm not aware of a library that does this for you. Maybe someone else can help you with this
You could try using an async block without specifying a dispatcher
o
@Caleb Kiage I mean multiplatform in a sense like a basic Ktor client - just an implementation in common code without having to write any platform-specific stuff
@Timon Herpens Okay, thanks! I'll try that out. Really strange though, since both the documentation and GitHub README list it as a feature for Kotlin/JVM only.
s
@Timon Herpens But I believe it's not available for Koltin/JS or Wasm. For that case I just make an `expect`/`actual` to store the dispatcher and provide
Dispatchers.Default
on web.
o
Thats really interesting - the documentation must be wrong in that case...?