How do I create a ReactiveCocoa subscriber that receives a signal only once, then unsubscribes/releases itself?

别来无恙 提交于 2019-12-03 10:08:32
[[RACAble(self.test) take:1] subscribeNext:^(id x) {
    NSLog(@"signal fired!");
}];

That might be helpful especially when you create nested subscriptions:

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

Little fix of kamil3 answer:

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

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];
   }];
}];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!