kotlinx.coroutines.channels

produce<Type> vs Channel<Type>()

删除回忆录丶 提交于 2019-12-23 05:04:21
问题 Trying to understand channels. I want to channelify the android BluetoothLeScanner. Why does this work: fun startScan(filters: List<ScanFilter>, settings: ScanSettings = defaultSettings): ReceiveChannel<ScanResult?> { val channel = Channel<ScanResult>() scanCallback = object : ScanCallback() { override fun onScanResult(callbackType: Int, result: ScanResult) { channel.offer(result) } } scanner.startScan(filters, settings, scanCallback) return channel } But not this: fun startScan(scope:

How can I send items to a Kotlin.Flow (like a Behaviorsubject)

爷,独闯天下 提交于 2019-11-29 20:10:29
问题 I wanted to know how can I send/emit items to a Kotlin.Flow , so my use case is: In the consumer/ViewModel/Presenter I can subscribe with the collect function: fun observe() { coroutineScope.launch { // 1. Send event reopsitory.observe().collect { println(it) } } } But the issue is in the Repository side, with RxJava we could use a Behaviorsubject expose it as an Observable/Flowable and emit new items like this: behaviourSubject.onNext(true) But whenever I build a new flow: flow { } I can