CombineLatest在Just vs Future中具有不同的行为

时间:2020-09-28 13:48:20

标签: swift combine

在Combine中,有(其中包括)这两个发布者:

new_stages = A.in_edge(c, data='stages')
stages = B.node[p]['stages']
stages.append(new_stages)
<<Update node p to have name of c??>> 
B.add_node(p, stages=new_stage_set)

这两个在放入let just = Just("Just") let future = Future<String, Never>({ (promise) in DispatchQueue.main.async { promise(.success("Future")) } }) 发布者中时具有不同的输出,如下所示

combineLatest

是否可以通过某种方式(例如,操作员)修改just.combineLatest([1,2,3].publisher).sink { (value) in print(value) }.store(in: &bin) /* prints ("Just", 1) ("Just", 2) ("Just", 3) */ future.combineLatest([1,2,3].publisher).sink { (value) in print(value) }.store(in: &bin) /* prints: ("Future", 3) */ 发布者,使其表现得像Future发布者?

1 个答案:

答案 0 :(得分:0)

Future的行为与您调用combineLatest时没有什么不同。由于您在Future中添加了异步分派,因此您的示例只是有缺陷。

combineLatest仅在其所有上游都发出值时才发出值。因此,如果在DispatchQueue.main.async内调用promise之前添加Future,则会延迟Future中的发射。因此,Future的发出要晚于Array.publisher,因此您只会看到{{1}的输出1(有时是0,具体取决于何时执行主线程上的下一个运行循环)。 }}。

删除异步调用后,您会从combineLatestFuture看到相同的输出。

Just