How do I create a ReactiveCocoa subscriber that re

2019-03-24 12:08发布

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?

3条回答
放荡不羁爱自由
2楼-- · 2019-03-24 12:49

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];
   }];
}];
查看更多
爷、活的狠高调
3楼-- · 2019-03-24 13:03
[[RACAble(self.test) take:1] subscribeNext:^(id x) {
    NSLog(@"signal fired!");
}];
查看更多
爷的心禁止访问
4楼-- · 2019-03-24 13:06

That might be helpful especially when you create nested subscriptions:

RACDisposable *subscription = [RACObserve(self, test) subscribeNext:^(id x) {
         NSLog(@"signal fired!");
}];
[subscription dispose];
查看更多
登录 后发表回答