这是关于RAC 2.0的问题,我很陌生:
我们说我有一个信号:
<receiver android:name=".AlarmReceiver" android:process=":remote" />
当我的应用程序启动时,我想要
- (RACSignal *)signalWithAsyncWorkInIt;
但是sig2的RACSignal *sig1 = [self.signalWithAsyncWorkInIt subscribeNext:...];
// then somewhere else later:
RACSignal *sig2 = [self.signalWithAsyncWorkInIt subscribeNext:...]; // looking for some option other than subscribeNext:
方法不会触发signalWithAsyncWorkInIt的执行,如果sig1已经或者只是要这样做,那么sig2只会重放&#34;最新结果& #34; of signalWithAsyncWorkInIt如果它存在并等待sig1等其他东西触发signalWithAsyncWorkInIt它是第一次它还没有被触发。
答案 0 :(得分:2)
我不确定我是否理解正确,但也许您正在寻找的是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/