ReactiveCocoa - 从未调用过的 flattenMap 块
ReactiveCocoa - flattenMap block never called
我在使用 RACSignal
的 flattenMap
方法时遇到问题 - 该块永远不会被调用。如果我 subscribeNext
到相同的信号,它工作得很好,问题只出现在 flattenMap
上。
以下是工作正常的方法
[[self.aSignal combineLatestWith:self.otherSignal] subscribeNext:^(RACTuple *tuple) {
// gets called just fine
}];
下面是不起作用的:
self.yetAnotherSignal = [[self.aSignal combineLatestWith:self.otherSignal] flattenMap:^RACStream *(RACTuple *tuple) {
// never gets called
return returnSignal;
}];
我错过了什么吗?还是我误解了 flattenMap
的工作原理?
您似乎只遗漏了一点点:(至少在您的代码段中)没有人订阅您的新信号!
您正在通过 combineLatest
和 flattenMap
从 self.aSignal
和 self.otherSignal
构造一个新信号 (self.yetAnotherSignal
)。
但是这个新信号,以及链中的任何操作符,在以某种形式订阅之前实际上不会做任何工作,最简单的形式是通过 subscribeNext
就像你在你的第一个片段。
不仅flattenMap
如此,任何操作都是如此,例如combineLatestWith
在你的第一个例子中如果你不订阅它就不会做任何事情。 map
、filter
、...也是如此。
我在使用 RACSignal
的 flattenMap
方法时遇到问题 - 该块永远不会被调用。如果我 subscribeNext
到相同的信号,它工作得很好,问题只出现在 flattenMap
上。
以下是工作正常的方法
[[self.aSignal combineLatestWith:self.otherSignal] subscribeNext:^(RACTuple *tuple) {
// gets called just fine
}];
下面是不起作用的:
self.yetAnotherSignal = [[self.aSignal combineLatestWith:self.otherSignal] flattenMap:^RACStream *(RACTuple *tuple) {
// never gets called
return returnSignal;
}];
我错过了什么吗?还是我误解了 flattenMap
的工作原理?
您似乎只遗漏了一点点:(至少在您的代码段中)没有人订阅您的新信号!
您正在通过 combineLatest
和 flattenMap
从 self.aSignal
和 self.otherSignal
构造一个新信号 (self.yetAnotherSignal
)。
但是这个新信号,以及链中的任何操作符,在以某种形式订阅之前实际上不会做任何工作,最简单的形式是通过 subscribeNext
就像你在你的第一个片段。
不仅flattenMap
如此,任何操作都是如此,例如combineLatestWith
在你的第一个例子中如果你不订阅它就不会做任何事情。 map
、filter
、...也是如此。