17

I'm currently registering a subscriber to a property signal like this:

[RACAble(self.test) subscribeNext:^(id x) {
        NSLog(@"signal fired!");
 }];

The default functionality is that it fires every single time self.test is changed, but I just want it to fire once, and then unsubscribe. Is there a "once" argument or modifier I can pass to RAC when I create this subscriber?

Cœur
  • 37,241
  • 25
  • 195
  • 267
zakdances
  • 22,285
  • 32
  • 102
  • 173

4 Answers4

32
[[RACAble(self.test) take:1] subscribeNext:^(id x) {
    NSLog(@"signal fired!");
}];
Josh Vera
  • 650
  • 7
  • 11
  • thanks, this prompted me to refactor a couple of signal generating methods into something much more sane. – Jon Aug 02 '13 at 13:55
0

That might be helpful especially when you create nested subscriptions:

RACDisposable *subscription = [RACObserve(self, test) subscribeNext:^(id x) {
         NSLog(@"signal fired!");
}];
[subscription dispose];
kamil3
  • 1,232
  • 1
  • 14
  • 19
0

Little fix of kamil3 answer:

__block RACDisposable *subscription = [RACObserve(self, test) subscribeNext:^(id x) {
    [subscription dispose];
    NSLog(@"signal fired!");
}];
-1

you can also do this (if you aren't into the whole brevity thing):

[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber){
   RACDisposable *inner_disposer = [RACAble(self.test) subscribeNext:^(id x){
      [subscriber sendNext:x];
      [subscriber sendComplete];
   }];
   return [RACDisposable disposableWithBlock:^{
      [inner_disposer dispose];
   }];
}];
Jon
  • 462
  • 4
  • 13