Hey there! I need some help with the kable library...
# juul-libraries
i
Hey there! I need some help with the kable library (or maybe with Kotlin concepts). I'm an iOS developer and pretty new to KMP and Kotlin in general. There's a custom KMP library that uses kable for managing Bluetooth connections, and I'm struggling with exception handling on the iOS client side. I marked the output function of the custom library with
Throws(Exception::class)
. Here's how it looks:
Copy code
@Throws(Exception::class) 
    fun deviceFlow(): Flow<BLEDevice> {
             return scanner.advertisements
                .filter { !it.name.isNullOrBlank() }
                .map {
                    BLEDevice(
                        name = it.name.orEmpty(),
                        advertisement = it
                    )
                }
        } 
    }
If I just throw an Exception in the body of this function, it works fine. But if I run it on a device without Bluetooth usage permission, the function
awaitPoweredOn()
throws an exception and crashes the app and does not propagate this exception further. And I would like to handle such exceptions on the client side. Thanks in advance!
t
What failure is not being propagated? I’m guessing your issue is that the failure is propagating via the
Flow
rather than the function. If that is the case, I would advise you to look into https://github.com/rickclephas/KMP-NativeCoroutines. It allows you more easily collect data from `Flow`s (from Swift) and handle failures that may be emitted as well.
Alternatively, rather than having your
deviceFlow
function return a
Flow
, you could have it return a single item (the item of interest). Essentially, you could use a terminal flow operator (e.g.
first
), as a terminal flow operator will propagate any flow exceptions.
i
Oh, I see, it makes sense. Many thanks!
👍 1