Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Kotlin事件总线触发两次_Android_Kotlin_Kotlin Coroutines - Fatal编程技术网

Android Kotlin事件总线触发两次

Android Kotlin事件总线触发两次,android,kotlin,kotlin-coroutines,Android,Kotlin,Kotlin Coroutines,我在kotlin中对eventBus使用此代码,但它会触发两次。我不知道为什么会这样 object EventBus { val bus: BroadcastChannel<Any> = BroadcastChannel<Any>(1) private val parentJob = Job() private val coroutineContext: CoroutineContext get() = parentJob + D

我在kotlin中对eventBus使用此代码,但它会触发两次。我不知道为什么会这样

object EventBus {
    val bus: BroadcastChannel<Any> = BroadcastChannel<Any>(1)

    private val parentJob = Job()
    private val coroutineContext: CoroutineContext
        get() = parentJob + Dispatchers.Default

    private val scope = CoroutineScope(coroutineContext)
    fun send(o: Any) {
        scope.launch {
            bus.send(o)
        }
    }

    inline fun <reified T> asChannel(): ReceiveChannel<T> {
        return bus.openSubscription().filter { it is T }.map { it as T }
    }
}
并按如下方式听(此代码运行两次)

var subscription=EventBus.asChannel()
var s=scope.launch{
subscription.consumereach{event->
Timber.i(“NetEvent${event.isConected}”)
currentNet=event.isConected
}
}
我更喜欢:

class事件总线{
private val events=MutableSharedFlow()
暂停娱乐调度(事件:任何){
events.emit(事件)
}
暂停乐趣开启(coroutineScope:coroutineScope,handler:suspend(Any)->Unit)=
coroutineScope.launch(start=CoroutineStart.UNDISPATCHED){
events.asSharedFlow().collect{event->handler(event)}
}
}
类EventBusTest{
@试验
有趣的测试{
val事件=ConcurrentLinkedQue()
运行阻塞{
val eventBus=eventBus()
val job1=eventBus.on(this){events.add(“1:$it”)}
事件总线调度(“a”)
val job2=eventBus.on(this){events.add(“2:$it”)}
事件总线调度(“b”)
job1.cancelAndJoin()
事件总线调度(“c”)
job2.cancelAndJoin()
}
assertEquals(列表(“1:a”、“1:b”、“2:b”、“2:c”)、events.toList())
}
}

它将帮助您:谢谢您的回答。它解决了我的问题
EventBus.send(NetEvent(false))
 var subscription = EventBus.asChannel<NetEvent>()
    var s = scope.launch {
        subscription.consumeEach { event ->

            Timber.i("NetEvent ${event.isConected}")
            currentNet = event.isConected
        }
    }
class EventBus {
    private val events = MutableSharedFlow<Any>()

    suspend fun dispatch(event: Any) {
        events.emit(event)
    }

    suspend fun on(coroutineScope: CoroutineScope, handler: suspend (Any) -> Unit) =
        coroutineScope.launch(start = CoroutineStart.UNDISPATCHED) {
            events.asSharedFlow().collect { event -> handler(event) }
        }
}

class EventBusTest {
    @Test
    fun testEventBus() {
        val events = ConcurrentLinkedDeque<String>()
        runBlocking {
            val eventBus = EventBus()

            val job1 = eventBus.on(this) { events.add("1: $it") }
            eventBus.dispatch("a")

            val job2 = eventBus.on(this) { events.add("2: $it") }
            eventBus.dispatch("b")

            job1.cancelAndJoin()

            eventBus.dispatch("c")

            job2.cancelAndJoin()
        }
        assertEquals(listOf("1: a", "1: b", "2: b", "2: c"), events.toList())
    }
}