Objective-C ReactiveCocoa 折叠(聚合)
Objective-C ReactiveCocoa fold (aggregate)
我有下一个密码
RACSubject *subject = [RACSubject subject];
[subject subscribeNext:^(id x) {
NSLog(@"A: %@", x);
}];
[[subject aggregateWithStart:@"" reduce:^id(NSString *running, NSString *next) {
return [running stringByAppendingString:next];
}] subscribeNext:^(id x) {
NSLog(@"B: %@", x);
}];
[subject sendNext:@"hello"];
[subject sendNext:@"world"];
但是
subscribeNext:^(id x) {
NSLog(@"B: %@", x);
}
不打电话,为什么?
以及如何解决?
当前日志:
2016-01-05 02:11:39.472 xxx[38643:7565618] A: hello
2016-01-05 02:11:39.472 xxx[38643:7565618] A: world
等待日志:
A: hello
A: world
B: hello world
p.s。 ReactiveCocoa 2.5
来自 aggregateWithStart:reduce:
的文档
Returns a signal that will send the aggregated value when the receiver completes, then itself complete.
关键是接收方需要完成,以便聚合到"finish"并发送它的值。所以你需要一个[subject sendCompleted];
语句。
如果这不是您想要的行为,有一个名为 combinePreviousWithStart:reduce:
的相关方法将为您提供如下输出:
A: hello
B: hello
A: world
B: helloworld
我有下一个密码
RACSubject *subject = [RACSubject subject];
[subject subscribeNext:^(id x) {
NSLog(@"A: %@", x);
}];
[[subject aggregateWithStart:@"" reduce:^id(NSString *running, NSString *next) {
return [running stringByAppendingString:next];
}] subscribeNext:^(id x) {
NSLog(@"B: %@", x);
}];
[subject sendNext:@"hello"];
[subject sendNext:@"world"];
但是
subscribeNext:^(id x) {
NSLog(@"B: %@", x);
}
不打电话,为什么? 以及如何解决?
当前日志:
2016-01-05 02:11:39.472 xxx[38643:7565618] A: hello
2016-01-05 02:11:39.472 xxx[38643:7565618] A: world
等待日志:
A: hello
A: world
B: hello world
p.s。 ReactiveCocoa 2.5
来自 aggregateWithStart:reduce:
Returns a signal that will send the aggregated value when the receiver completes, then itself complete.
关键是接收方需要完成,以便聚合到"finish"并发送它的值。所以你需要一个[subject sendCompleted];
语句。
如果这不是您想要的行为,有一个名为 combinePreviousWithStart:reduce:
的相关方法将为您提供如下输出:
A: hello
B: hello
A: world
B: helloworld