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

Francis Mariano

02/11/2021, 6:01 PM
Hello guys. I have a BluetoothRepository which is responsible to emit a SharedFlow. I have also a DeviceScanPresenter which is responsible for collecting the emitted data by BluetoothRepository. However, the data is emitted with success, but the DeviceScanPresenter does not collect the data. Note: This code works like a charm on ANDROID device. The problem described above happens just on iOS device. Can anyone give a tip about the problem? DeviceScanPresenter
Copy code
class DeviceScanPresenter(
    private val bluetoothRepository: BluetoothRepositoryKmm
) : BasePresenter<DeviceScanView>() {

    private val mainScope = MainScope()
    private var scan: Job? = null

    .......

    fun deviceScanDevice() {
        view?.log("presenter - deviceScanDevice444")
        view?.showCancelButton()
        view?.clearDevices()
        view?.log("presenter - deviceScanDevice:beforeOfLaunch")
        scan = mainScope.launch(start = CoroutineStart.UNDISPATCHED) {
            view?.log("presenter - deviceScanDevice:launch")
            try {
                view?.log("presenter - deviceScanDevice:try")
                withTimeout(20000) {
                    view?.log("presenter - deviceScanDevice:withTimeout:withTimeout")
                    view?.log("presenter - deviceScanDevice:withTimeout:bluetoothRepository = $bluetoothRepository")
                    bluetoothRepository.devices.collect {
                        view?.log("presenter - deviceScanDevice:withTimeout:collect")
                        view?.showDevice(it)
                    }
                }
            } catch (e: TimeoutCancellationException) {
                view?.log("presenter - deviceScanDevice:catch : ${e.message}")
                view?.showUpdateButton()
            }
        }.apply {
            invokeOnCompletion {
                bluetoothRepository.stopScanLeDevice()
                view?.log("presenter - deviceScanDevice444:invokeOnCompletation")
            }
        }
        view?.log("presenter - deviceScanDevice:startScanLeDevice")
        bluetoothRepository.startScanLeDevice()
    }
    
    .....
}
BluetoothRepositoryKmm
Copy code
object BluetoothRepositoryKmmImpl : BluetoothRepositoryKmm {

    ....

    private var repositoryScope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)

    ....

    private val _devices = MutableSharedFlow<Device>()
    override val devices = _devices.asSharedFlow()

    ....

    override fun startScanLeDevice() {
        println("BluetoothRepositoryKmmImpl - startScanLeDevice333")
        scanJob = repositoryScope.launch {
            println("BluetoothRepositoryKmmImpl - startScanLeDevice:launch")
            Scanner().advertisements.collect {
                println("BluetoothRepositoryKmmImpl - startScanLeDevice:collect")
                val ret = _devices.tryEmit(it.devicePlatformFrom())
                println("BluetoothRepositoryKmmImpl - startScanLeDevice:collect:ret = $ret")
            }
        }.apply { invokeOnCompletion { println("BluetoothRepositoryKmmImpl - startScanLeDevice:invokeOnCompletation") } }
    }

    ....

}
Logs: BluetoothRepositoryKmmImpl - startScanLeDevice333 BluetoothRepositoryKmmImpl - startScanLeDevice:launch BluetoothRepositoryKmmImpl - startScanLeDevice:collect BluetoothRepositoryKmmImpl - startScanLeDevicecollectret = true BluetoothRepositoryKmmImpl - startScanLeDevice:collect BluetoothRepositoryKmmImpl - startScanLeDevicecollectret = true BluetoothRepositoryKmmImpl - startScanLeDevice:collect BluetoothRepositoryKmmImpl - startScanLeDevicecollectret = true BluetoothRepositoryKmmImpl - startScanLeDevice:collect BluetoothRepositoryKmmImpl - startScanLeDevicecollectret = true BluetoothRepositoryKmmImpl - startScanLeDevice:collect BluetoothRepositoryKmmImpl - startScanLeDevicecollectret = true BluetoothRepositoryKmmImpl - stopScanLeDevice BluetoothRepositoryKmmImpl - startScanLeDevice:invokeOnCompletation
DeviceScanViewController:log = presenter - onViewAttached DeviceScanViewController:log = presenter - deviceScanDevice444 DeviceScanViewController:log = presenter - deviceScanDevice:beforeOfLaunch DeviceScanViewController:log = presenter - deviceScanDevice:launch DeviceScanViewController:log = presenter - deviceScanDevice:try DeviceScanViewController:log = presenter - deviceScanDevicewithTimeoutwithTimeout DeviceScanViewController:log = presenter - deviceScanDevicewithTimeoutbluetoothRepository = br.com.stetsom.stx2436bt.data.repositories.BluetoothRepositoryKmmImpl@808e80e8 DeviceScanViewController:log = presenter - deviceScanDevice:startScanLeDevice DeviceScanViewController:log = presenter - deviceScanDevice:catch : Timed out waiting for 20000 ms DeviceScanViewController:log = presenter - deviceScanDevice444:invokeOnCompletation
3 Views