Reactivecocoa 忽略对函数的进一步调用,直到上一次调用完成

Reactivecocoa ignore further calls to a function until previous call to it has completed

在我的 reactive cocoa 中,如果之前对它的调用仍在进行,我想阻止对函数的调用。我已经按如下方式实现了这一点,但它看起来更像是一个 hack。

 __block RACSignal * performSync = [[self performSync:connectionClient] take:1];

[[[self rac_signalForSelector:@selector(forceSync:)]]]
   flattenMap:^RACStream *(id value) {
       NSLog(kMILogLevelDebug, @"Perform sync requested");
       return performSync;
   }]
   subscribeNext:^(id x) {
      NSLog(kMILogLevelDebug,@"Sync is performed", [NSDate date]);
   }
   error:^(NSError *error) {
       [self performSyncCompleted:error];
   }
   completed:^{
       [self performSyncCompleted:nil];
       performSync = [[self performSync:connectionClient] take:1];

   }];

所以,我创建了一个 performSync 信号,它只执行一次,一旦完成,我就重新创建信号。有没有更好的方法来完成上述任务?

RACSignal 中的方法 waitUntilCompleted 可以解决问题。

我认为您应该使用 RACCommand :

RACCommand* command = [[RACCommand alloc] initWithSignalBlock:^(id _) {
    return [#mysignal];
}];
// then later
[command execute:nil];
// or
[[command execute:nil] take:1];// synchronous but be careful ! here be dragons

您可以根据需要多次调用 execute,一次不会多次订阅信号。它在默认设置中广泛使用多播信号。

此外,您可以通过以下方式了解命令是否正在执行:

[command executing];

here is a blog article talking about RACCommands