RAC:如何延迟订阅信号,即如果该信号已经或将要被其他订阅者执行,则不触发它的执行?
RAC: how to subscribe to signal lazily i.e. do not trigger its execution if that signal already was or going to be executed by other subscriber?
这是关于 RAC 2.0 的问题,我对它还很陌生:
假设我有一个信号:
- (RACSignal *)signalWithAsyncWorkInIt;
当我的应用启动时,我想做
RACSignal *sig1 = [self.signalWithAsyncWorkInIt subscribeNext:...];
// then somewhere else later:
RACSignal *sig2 = [self.signalWithAsyncWorkInIt subscribeNext:...]; // looking for some option other than subscribeNext:
但是如果 sig1 已经执行或即将执行,sig2 的 subscribeNext:
方法将不会触发 signalWithAsyncWorkInIt 的执行,即这样 sig2 只会重播 signalWithAsyncWorkInIt 的 "latest result" case 如果它存在并等待像 sig1 这样的东西第一次触发 signalWithAsyncWorkInIt 如果它还没有被触发。
我不确定我是否理解正确,但也许您要查找的是 replay
、replayLast
或 replayLazily
.
通过回放,您可以避免多次执行一个信号。它将结果多播给多个订阅者,而不是对每个订阅者都执行操作。
简单示例:
RACSignal* asyncOperationSignal = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"execute something here and return with the result");
[subscriber sendNext:@"42"];
return nil;
}]
replayLazily]; // call replayLazily to make sure the operation only executes after the first subscription
// and returns the result for every consecutive subscription
[asyncOperationSignal subscribeNext:^(NSString* result) {
NSLog(@"Result: %@", result);
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// subscribe again later in the code
[asyncOperationSignal subscribeNext:^(NSString* result) {
NSLog(@"Result: %@", result);
}];
});
输出将是:
execute something here and return with the result
42
42
这是一篇关于此主题的文章,写得非常好:
http://spin.atomicobject.com/2014/06/29/replay-replaylast-replaylazily/
这是关于 RAC 2.0 的问题,我对它还很陌生:
假设我有一个信号:
- (RACSignal *)signalWithAsyncWorkInIt;
当我的应用启动时,我想做
RACSignal *sig1 = [self.signalWithAsyncWorkInIt subscribeNext:...];
// then somewhere else later:
RACSignal *sig2 = [self.signalWithAsyncWorkInIt subscribeNext:...]; // looking for some option other than subscribeNext:
但是如果 sig1 已经执行或即将执行,sig2 的 subscribeNext:
方法将不会触发 signalWithAsyncWorkInIt 的执行,即这样 sig2 只会重播 signalWithAsyncWorkInIt 的 "latest result" case 如果它存在并等待像 sig1 这样的东西第一次触发 signalWithAsyncWorkInIt 如果它还没有被触发。
我不确定我是否理解正确,但也许您要查找的是 replay
、replayLast
或 replayLazily
.
通过回放,您可以避免多次执行一个信号。它将结果多播给多个订阅者,而不是对每个订阅者都执行操作。
简单示例:
RACSignal* asyncOperationSignal = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"execute something here and return with the result");
[subscriber sendNext:@"42"];
return nil;
}]
replayLazily]; // call replayLazily to make sure the operation only executes after the first subscription
// and returns the result for every consecutive subscription
[asyncOperationSignal subscribeNext:^(NSString* result) {
NSLog(@"Result: %@", result);
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// subscribe again later in the code
[asyncOperationSignal subscribeNext:^(NSString* result) {
NSLog(@"Result: %@", result);
}];
});
输出将是:
execute something here and return with the result
42
42
这是一篇关于此主题的文章,写得非常好: http://spin.atomicobject.com/2014/06/29/replay-replaylast-replaylazily/