Reputation: 83
In my reactive cocoa, I want to block calls to a function if a previous call to itis still progress. I have achieved this as follows, but it seems more like a 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];
}];
So, I created a performSync signal, which is executed only once, and once it completes, I recreate the signal. Any better way to accomplish the above?
Upvotes: 0
Views: 565
Reputation: 1321
You should in my opinion use 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
You can call execute as many time as you want, it wont subscribe the signal more than once at a time. It makes extensive use of multicasted signals in the default setup.
Moreover you can know if the command is executing by using:
[command executing];
here is a blog article talking about RACCommands
Upvotes: 2