考虑以下片段:
- (RACSignal *)startRouting {
...
}
- (RACSignal *)updateRoutingWithSession:(NSString *)session {
...
}
- (RACSignal *)fetchFlights {
return [[self startRouting] flattenMap:^RACStream *(NSString *session) {
return [[[[self updateRoutingWithSession:session]
delay:2.0f]
repeat]
takeUntilBlock:^BOOL(RACTuple *operationAndResponse) {
AFHTTPRequestOperation *operation = [operationAndResponse first];
NSDictionary *response = [operationAndResponse second];
return [operation isCancelled] || 100 == [response[kPercentComplete] intValue];
}];
}];
}
这里发生的事情是 startRouting
返回一个发送 session ID 的 RACSignal
。
updateRoutingWithSession:
返回一个 RACSignal
,它发送一个包含 PercentComplete
属性的 NSDictionary
。轮询之间有两秒的延迟。
fetchFlights
将一直运行到 updateRoutingWithSession:
的 PercentComplete
为 100。
我的问题是最后一个 sendNext:
,takeUntilBlock
返回 true
,没有到达 RACSubscriber
。
我错过了什么?
最佳答案
我在 RX 的世界里发现了这个。这通常通过合并两个信号来解决。一个采用重复源直到谓词为真。另一个在谓词为真时跳过。
看起来像这样
BOOL (^finished)(id _) = ^BOOL(id _) {
return predicate; // BOOLean here
}
// You want a multicast signal, because multiple signals will subscribe to the source.
// Multicasting it means that you won't get repeated api-requests, in this case.
RACMulticastConnection *source = [[theSignal repeat] publish];
RACSignal *whileNotDone = [source.signal takeUntilBlock:finished];
RACSignal *whenDone = [[source.signal skipUntilBlock:finished] take:1];
RACSignal *merged = [RACSignal merge:@[whileNotDone, whenDone]];
[source connect]; // Needed for a multicast signal to initiate.
merged
信号将 sendNext
source
中的每个 next
包括最后一个。然后sendCompleted
。
RX 世界的一些引用资料:
关于objective-c - ReactiveCocoa takeUntil 和 takeWhile 不发送最后一个 "next",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15785142/