Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.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
Rx java groupBy运算符,来自不同组的项目交错_Rx Java - Fatal编程技术网

Rx java groupBy运算符,来自不同组的项目交错

Rx java groupBy运算符,来自不同组的项目交错,rx-java,Rx Java,以下代码: Observable .just(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) .doOnNext(item -> System.out.println("source emitting " + item)) .groupBy(item -> { System.out.println("groupBy called for " + item);

以下代码:

    Observable
            .just(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
            .doOnNext(item -> System.out.println("source emitting " + item))
            .groupBy(item -> {
                System.out.println("groupBy called for " + item);
                return item % 3;
            })
            .subscribe(observable -> {
                System.out.println("got observable " + observable + " for key " + observable.getKey());
                observable.subscribe(item -> {
                    System.out.println("key " + observable.getKey() + ", item " + item);
                });
            });
让我感到困惑。我得到的结果是:

    source emitting 0
    groupBy called for 0
    got observable rx.observables.GroupedObservable@42110406 for key 0
    key 0, item 0
    source emitting 1
    groupBy called for 1
    got observable rx.observables.GroupedObservable@1698c449 for key 1
    key 1, item 1
    source emitting 2
    groupBy called for 2
    got observable rx.observables.GroupedObservable@5ef04b5 for key 2
    key 2, item 2
    source emitting 3
    groupBy called for 3
    key 0, item 3
    source emitting 4
    groupBy called for 4
    key 1, item 4
    source emitting 5
    groupBy called for 5
    key 2, item 5
    source emitting 6
    groupBy called for 6
    key 0, item 6
    source emitting 7
    groupBy called for 7
    key 1, item 7
    source emitting 8
    groupBy called for 8
    key 2, item 8
    source emitting 9
    groupBy called for 9
    key 0, item 9
因此,在顶层subscribe方法中,我从GroupedObservable中获得了3个观察值,正如预期的那样。然后,一个接一个地,我订阅了分组的观测值——这里有一件事我不明白:

为什么原始项目仍按原始顺序(即0、1、2、3、…)而不是0、3、6、9、…)发射。。。对于键0,键1后接1、4、7,键2后接2、5、8

我想我了解这些小组是如何创建的:

1. 0 is emitted, the key function is called and it gets 0
2. it is checked if an observable for 0 exists, it doesn't, so a new one is created and emitted, and then it emits 0
3. the same happens for source items 1 and 2 as they both create new groups, and observables with key 1 and 2 are emitted, and they emit 1 and 2 correspondingly
4. source item 3 is emitted, the key function is called and it gets 0
5. it is checked if an observable for 0 exists, it does -> no new grouped observable is created nor emitted, but 3 is emitted by the already existing observable
6. etc. until the source sequence is drained
看起来,虽然我一个接一个地得到了分组的观测值,但它们的发射是以某种方式交错的。这是怎么发生的

为什么原始项目仍按原始顺序(即0、1、2、3、…)而不是0、3、6、9、…)发射。。。对于键0,键1后接1、4、7,键2后接2、5、8

你已经回答了你自己的问题。您正在对项目流进行操作,按照它们发出的顺序进行操作。因此,当每一个都被发出时,它会被传递到操作符链中,您可以看到这里显示的输出

您期望的替代输出要求链等待源停止为所有组发送项。假设你有
可观察到的。只有(0,1,2,3,4,4,4,4,0)
。然后你会期望(0,3,0),(1,4,4,4,4),(2)作为你的输出组。如果你有一个4的无限流呢?您的订户将永远不会收到该0,3。。来自第一组

你可以创造你想要的行为。
toList
操作符将缓存输出,直到源完成,然后将
列表
传递给订阅服务器:

.subscribe(observable -> {
    System.out.println("got observable " + observable + " for key " + observable.getKey());
    observable.toList().subscribe(items -> {
        // items is a List<Integer>
        System.out.println("key " + observable.getKey() + ", items " + items);
    });
});
.subscribe(可观察->{
System.out.println(“got observable”+observable+“for key”+observable.getKey());
observable.toList().subscribe(项目->{
//项目是一个列表
System.out.println(“key”+observable.getKey()+”,items“+items);
});
});

我知道toList()技巧。我也知道这种行为是有道理的,你的例子很好地说明了原因。我对它的工作方式如何实现感到相当困惑。我得到3个观测值,我订阅它们,因为它们是由分组发出的——但是它怎么能被交错呢?这就好像这3个订阅是立即发生的,而随后的发射是异步的。你能解释一下吗?当一个新的“组”按需创建时,不同组的3个订阅就会发生。排放实际上是同步发生的——因为这都在同一个线程上,每一个都必须在处理下一个之前完成处理。我想这可能是你错过的关键点吧?“都在同一个线程上,每个线程都必须完成处理,然后才能处理下一个线程”——这正是我不明白的。输出表明,所有类型的数据都是同时处理的;并不是第一组先被消耗,然后下一组进入画面-它们的发射是相互交错的。将
groupBy
视为一种路由机制,因此当每个项目都是从第一个可观察对象发射时,
groupBy
函数将该项目路由到适当的观察者(必要时创建)。实际上显示了您预期的时间轴行为,我认为,这是不幸的(它显示了三角形之前的所有圆圈)。是的,大理石图让事情变得混乱。但是,我想我明白了,你的路由提示和我自己在代码帮助下的尝试和错误。因此,基本上,我理解它的方式是:一旦一个新的分组观测值被首次创建,我可以订阅它,它说:“我对这个组中的发射感兴趣”。然后,对于每个数字,一个为订阅服务器检查组(新创建或查找),如果存在订阅服务器,则调用其onNext()。