ZIP运算符不适用于PublishSubject,我在做什么错?

时间:2019-06-25 19:34:20

标签: android rx-java2

我是RxJava的新手,无法意识到–当我将其与两个PublishSubject一起使用时,为什么我的“压缩”可观察对象不发射项目? (据我所知,ZIP运算符应将两个流“合并”为一个)

val currentSubject = PublishSubject.create<Int>()
val maxSubject = PublishSubject.create<Int>()

currentSubject.onNext(1)
maxSubject.onNext(2)

currentSubject.onNext(1)
maxSubject.onNext(2)

Log.d("custom", "BINGO!")

val zipped = Observables.zip(currentSubject, maxSubject) { current, max -> "current : $current, max : $max " }
zipped.subscribe(
    { Log.d("custom", it) },
    { Log.d("custom", "BONGO!") },
    { Log.d("custom", "KONGO!") }
)

currentSubject.onComplete()
maxSubject.onComplete()

我希望这些项目显示在“ {Log.d(” custom“,it)}”函数中,但不会发生。我在做什么错了?

编译后登录:

2019-06-25 22:25:36.802 3631-3631 / ru.grigoryev.rxjavatestdelete在D / custom之后:BINGO!

2019-06-25 22:25:36.873 3631-3631 / ru.grigoryev.rxjavatestdelete D / custom之后:KONGO!

1 个答案:

答案 0 :(得分:1)

这里的问题与您的PublishSubject实现无关,而与Obervables的默认行为有关。但首先,让我们备份

冷热观测

在Rx中,有{em {em}}的两种类型:hotcoldcold。最常见的类型是cold可观察的类型。 .subscribe()的对象在调用val obs = Observable.fromIterable(listOf(1, 2, 3, 4); obs.subscribe { print(it) } // Prints 1, 2, 3, 4 之前不会开始发出值。

hot

无论观察者是否订阅,val subject = PublishSubject.create<Int>() subject.onNext(1) subject.onNext(2) subject.subscribe { print(it) } subject.onNext(3) subject.onNext(4) // Prints 3, 4 都将发出值。

1

注意如何2PublishSubject不打印。这是因为hot1可以观察到的,并且在订阅之前会发出2zipped

返回您的问题

在您的示例中,您发布的主题在订阅之前发出1和2 。要一起查看他们val currentSubject = PublishSubject.create<Int>() val maxSubject = PublishSubject.create<Int>() Log.d("custom", "BINGO!") val zipped = Observables.zip(currentSubject, maxSubject) { current, max -> "current : $current, max : $max " } zipped.subscribe( { Log.d("custom", it) }, { Log.d("custom", "BONGO!") }, { Log.d("custom", "KONGO!") } ) currentSubject.onNext(1) maxSubject.onNext(2) currentSubject.onNext(1) maxSubject.onNext(2) currentSubject.onComplete() maxSubject.onComplete() ,请四处移动代码。

cursor.execute('select * from MSDB')
cursor.execute('''
    INSERT INTO MSDB ([SalesOrder], [SiteNumber], [SiteName], [Customer], [ShipDate])
    VALUES(SO, SNM, Custom, SD, SN)
              ''')
conn.commit()

else:
    continue