Pooja Bansiya
Pooja Bansiya

Reputation: 83

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

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

Answers (2)

kamidude
kamidude

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

user4439795
user4439795

Reputation: 1

Method waitUntilCompleted from RACSignal could do the trick.

Upvotes: -1

Related Questions